Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2004, 05:55 AM
Registered User
 
Join Date: Jul 2004
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,

 
Old July 7th, 2004, 12:03 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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".
 
Old July 15th, 2004, 01:53 PM
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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);

    try
    {
        Click(sender,arg);
    }
    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.





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





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