Solved

C# EventHandler class and hooking up events

Posted on 2012-03-13
4
402 Views
Last Modified: 2012-08-14
When I first learned about hooking up events, it looked something like this:

obj.Event += Handling_Method;

But then I started seeing an EventHandler class, like this:

obj.Event += EventHandler(Handling_Method);

What's the difference?
0
Comment
Question by:XTO
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 37715983
The C# compiler will implicitly convert a method to a delegate for you so long as the signature of the method matches the signature of the delegate associated with the event. Functionally, there is no difference.
0
 

Author Comment

by:XTO
ID: 37716173
Thank you kaufmed,

Is there a difference in advantage of using one way over another?

I know that the EventHandler class may have the advantage of a generic version.
So, we can code:

obj.Event1 += EventHandler<NewEventArgs>(Handling_Method);

But then, it seems we can get the same effect by putting NewEventArgs into the delegate definition, like this:

public void delegate delgHandler(object sender, NewEventArgs);
event delgHanlder Event1;
Event1 += Handling_Method;

I'm trying to understand why I would want to use one way over another.

Maybe the only advantage is for intellisense.

If I have:
item.MouseButtonUp +=
Then intellisense will offer me a RoutedEventHandler.
If I then click tab twice, then Visual Studio will create a method with the correct parameters.

Is that the only advantage? Or is there some other reason to use one way over the other?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37716269
I can't say that I've ever used the generic version. I usually just use the delegate defined for the event. For instance, if I were going to handle the KeyDown event for a Forms control, I might do:

public Form1()
{
    this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
}

void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    // logic
}

Open in new window


...where KeyEventHandler is the delegate which defines the signature a method that will function as the event's handler should have--in this case, the method should take two parameters:  an object, and a KeyEventArgs.

The EventHandler class (theoretically) serves as the base class for all event handler delegate objects within .NET. Likewise, the EventArgs serves as the base class for all event argument parameters. If you followed that model, then you could create functions which took in types of either EventHanlder or EventArgs and pass derived objects in to do some work. For example, you could create a function which took in EventArgs:

void LogEventArgType(EventArgs e)
{
    System.Diagnostics.Debug.WriteLine(e.GetType().ToString());
}

Open in new window


...and wrote the type of arg out to the Output window. In each of your handlers, you could call this function, passing in the current EventArgs:

void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    LogEventArgType(e);
}

Open in new window


Why would you do this? I don't have the slightest, but it's possible. In this case, your declaring one of your function's parameters to be the general base class in order to accept all derived types. I don't see much value in declaring the reference to the derived instance to be of the base class. Someone else might have some real-world encounters to say otherwise.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37716273
P.S.

You mentioned RoutedEventHandler, which as I understand is a WPF-specific type. I have no creditable experience in WPF land, so there may be situations in that environment where a general definition is more desirable. Unfortunately, I cannot say as to when.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question