Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Event handling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 10 Mar 2008 00:17 GMT
I have a sample program here that has one event with 2 different event
handlers (onButtonAction and onButtonAction2).

I had defined the event as:

   public event ButtonEventHandler ButtonClick;

where ButtonEventHandler is a delegate type with 2 parameters: one is an
object and the other is an int.

I then do:
       b.ButtonClick += new ButtonEventHandler(onButtonAction);
       b.ButtonClick += new ButtonEventHandler(onButtonAction2);

       b.ButtonClick += new ButtonEventHandler(onButtonAction);
       b.ButtonClick += new ButtonEventHandler(onButtonAction2);

I have added 4 event handlers to the ButtonClick event and they all have the
same type (object and int) as defined by the delegate:

delegate void ButtonEventHandler(object source, int clickCount);

I was just curious if you an event handler can only have one delegate (same
parameters)?

I can have as many handlers as I want (not sure why I would need to) but
they all have to be the same delegate type.

Is this always the case?

****************************************
using System;

//declaring the event handler delegate
delegate void ButtonEventHandler(object source, int clickCount);

class Button
{
   //declaring the event
   public event ButtonEventHandler ButtonClick;

   //Function to trigger the event
   public void clicked(int count)
   {
       Console.WriteLine("\nInside Clicked !!!");
       //Invoking all the event handlers
       if (ButtonClick != null) ButtonClick(this, count);
   }
}
public class Dialog
{
   public Dialog()
   {
       Button b = new Button();

       //Adding an event handler
       b.ButtonClick += new ButtonEventHandler(onButtonAction);
       //Triggering the event
       b.clicked(1);

       b.ButtonClick += new ButtonEventHandler(onButtonAction2);
       b.clicked(1);

       //Removing an event handler
       b.ButtonClick += new ButtonEventHandler(onButtonAction);
       b.clicked(1);

       b.ButtonClick += new ButtonEventHandler(onButtonAction2);
       b.clicked(1);
   }
   static void Main()
   {
       new Dialog();
       Console.Read();
   }

   //Event Handler function
   public void onButtonAction(object source, int clickCount)
   {
       Console.WriteLine("Inside Event Handler !!!");
   }
   public void onButtonAction2(object source, int clickCount)
   {
       Console.WriteLine("Inside Event Handler 2 !!!");
   }
}
****************************************

Thanks,

Tom
Peter Duniho - 10 Mar 2008 00:32 GMT
> [...]
> I can have as many handlers as I want (not sure why I would need to) but
> they all have to be the same delegate type.
>
> Is this always the case?

Yes.  When you declare the event, you have to provide the delegate type  
defining the signature for the event.  Only delegate instances that match  
that signature can be subscribed to the event (and in fact the default  
implementation is simply to keep a reference to the current delegate value  
in a private field with the same name as the event).

Pete
tshad - 10 Mar 2008 03:16 GMT
>> [...]
>> I can have as many handlers as I want (not sure why I would need to) but
[quoted text clipped - 7 lines]
> implementation is simply to keep a reference to the current delegate value
> in a private field with the same name as the event).

What do you mean the "default implementation"? and the "keep a reference to
the current delegate value  in a private field with the same name as the
event"

Also, where is the standard place to put the delegate declaration?  In my
example, I have it outside of my classes, but I have seen it used inside of
a class and called by a.delegate().

Thanks,

Tom

> Pete
Peter Duniho - 10 Mar 2008 06:18 GMT
> What do you mean the "default implementation"? and the "keep a reference  
> to
> the current delegate value  in a private field with the same name as the
> event"

See Jon Skeet's article: http://www.yoda.arachsys.com/csharp/events.html

Or, for that matter, the MSDN discussion on events.  You might start here:
http://msdn2.microsoft.com/en-us/library/edzehd2t.aspx

This page discusses more specifically what happens when you declare an  
event:
http://msdn2.microsoft.com/en-us/library/wkzf914z.aspx

> Also, where is the standard place to put the delegate declaration?  In my
> example, I have it outside of my classes, but I have seen it used inside  
> of
> a class and called by a.delegate().

I think that depends on how the delegate is used.  I think that a nested  
type makes sense when it is some way specifically part of the containing  
type.  For classes, this often means you want to be able to access members  
of the containing class, but that's not an issue for delegate types.  So  
it would be more an issue that the delegate type only makes sense in the  
context of the containing class.

If that's not the case, I'd declare the delegate type outside the class.

That assumes, of course, that there's some point in declaring the delegate  
type at all.  For example, if you are declaring your own EventArgs-derived  
class for the event, then you'll want a matching delegate type that uses  
that type.  But for basic events, often you can just use the .NET  
EventHandler type, and sometimes for more specific events you can still  
share an existing EventArgs-derived class and thus the associated delegate  
type.

And of course, there's no requirement that you use the EventArgs pattern  
for events.  Even though you should always consider using that pattern  
because of its consistency with the rest of .NET, you may find that your  
event delegate type matches some existing delegate type from .NET (if  
nothing else, a general-purpose delegate type for the event can be  
declared using the generic Action<> or Func<> types).

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.