Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# EventHandler class and hooking up events

Posted on 2012-03-13
4
Medium Priority
?
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

598 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