Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2004, 05:55 AM
Registered User
Join Date: Jul 2004
Location: Amsterdam, , Netherlands.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Customised events in C#

I have created a class that can raise events. Originally it was written for a specific purpose, and I added the events required. Now I want to use it for a more limited application, and I've run into a problem with some of the events.

It seems that where I haven't subscribed to the event in the user application, an exception is thrown when I try to raise the event. I can trap the exception, and just lose it, but does anyone know if there's anyway to check if an event had any subscribers before trying to raise it.

At the moment, if I stop the execution with the debugger, the Event delegate is just 'Undefined'.

Any ideas?

Thanks in advance,

Reply With Quote
  #2 (permalink)  
Old July 7th, 2004, 12:03 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

I'm not sure I understand what you mean by "subscribers". Do you mean that you use the class but don't have any methods handling the class' event? I don't think this should be a problem. You should be able to raise events of a class regardless of if anything is handling that event. This sounds like there is something else going on particularly because you state that the delegate is "undefined".
Reply With Quote
  #3 (permalink)  
Old July 15th, 2004, 01:53 PM
Registered User
Join Date: Jul 2004
Location: Amsterdam, , Netherlands.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Sorry for the delay, but I got sidetracked on something else.

I based my original class on the suggestions in the help file for faking a control array, which I use to have an array of usercontrols based on a textbox.

There is an AddBox procedure, which adds a new box to the array, and subscribes to the various events that I want to pass on to the main application, such as

box.Click +=new EventHandler(box_Click);

where box is the new text box, and box_Click is as follows:

private void box_Click(object sender, EventArgs e)
    MyBoxEventArgs arg;
    Coordinates c;

    c = (Coordinates)((Mybox)sender).Tag;

    //adjust the row and column to make them 1-based
    arg = new MyBoxEventArgs(c.Row+1, c.Col+1);

    catch {}


Briefly, the tag contains the x and y coordinates of the box in the array.

I reused the array on another form in the application, but this time I wasn't interested in the Click event, so never wrote a handler for it. Without the try...catch block, this threw an uncaught exception. Hovering the mouse oer the Click in the line Click(sender,arg), the debugger suggests that Click is an 'undefined value'.

Conversely when you reach the same point having clicked the box on the original point, Click is an event handler according to the debugger.

My question is whether there isn't a better way of dealing with this than a try...catch block? It ought to be possible to avoid the exception being thrown in the first place as there is a certain overhead involved in raising it and catching it.

Thanks for any help.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Customised Error Messages denzil_cactus Perl 3 June 15th, 2007 10:27 AM
Customised Experience with Themes rit01 ASP.NET 2.0 Professional 0 August 3rd, 2006 09:18 AM
Adding customised rows to datagrid danielg C# 2 April 11th, 2005 05:35 PM
Customised XML Editor udaysai XML 0 February 21st, 2005 03:52 AM
Send Form To Customised Dll Israr .NET Framework 2.0 0 December 9th, 2004 02:00 AM

All times are GMT -4. The time now is 07:57 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.