Solved

C# EventHandler class and hooking up events

Posted on 2012-03-13
4
399 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now