Event handling

T

tshad

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
 
P

Peter Duniho

[...]
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
 
T

tshad

Peter Duniho said:
[...]
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).

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
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top