Solved

C# EventHandler class and hooking up events

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

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 74

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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

19 Experts available now in Live!

Get 1:1 Help Now