Solved

C# EventHandler class and hooking up events

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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 …

803 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