Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone please explain to me in simple succinct terms the concept of using a custom EventHandler delegate and the in-built EventHandler delegate?

In a text book(c# 4.0 in a nut shell by o'reilly) i was reading it had this to say about the predefined nongeneric EventHandler delegate: "it (EventHandler delegate) can be used when an event doesn't carry extra information."

"...when an event doesn't carry extra information." What does this part mean? Is it when the handler method has no need to process or pass-on any data?

also:
I have seen in different scenerio where one of the following is used to fire an event. What warrants one using one type than the other?
a. EventName(this, e)
b. EventName(new object(), e)
c. EventName(this, new EventArgs())

If you know of any other please be kind to bed in your explanation. Thanks.
Posted

1 solution

A standard EventHandler supplies only two items to the method handling the event: the object that caused the event to be fired (the sender parameter) and the "normal", basic EventArgs (the e parameter). It you don't need to supply more than this, then you can use the Eventargs to create a simple, quick-to-create Event in your class that the outside world can subscribe to:
C#
/// <summary>
/// Event to indicate MyEvent happened
/// </summary>
public event EventHandler MyEvent;
/// <summary>
/// Called to signal to subscribers that MyEvent happened
/// </summary>
/// <param name="e"></param>
protected virtual void OnMyEvent(EventArgs e)
    {
    EventHandler eh = MyEvent;
    if (eh != null)
        {
        eh(this, e);
        }
    }

If you need to provide more than this, then you have to use a Custom EventHandler with your own (derived) EventArgs:
C#
public partial class MyForm : Form
    {
    public event EventHandler<ChangedArgs> Changed;

    private void butGo_Click(object sender, EventArgs e)
        {
       EventHandler ch = Changed;
       if (ch != null)
          {
          ch(this, new ChangedArgs(tbData.Text));
          }
        }
    }

public partial class ChangedArgs : EventArgs
    {
    public string strData;
    public ChangedArgs(string str)
        {
        strData = str;
        }
    }


In answer to your other question:
a) When you want to pass the existing events up from a event handler in your class to the handler for your event. For example, in a Click event, you might pass the EventArgs that your button handler gets to the handler for your classes "DataReady" event.
b) When you don't want to pass the actual class instance to the event handler, but need to pass an object of some form (or pretend it came from a different object that it really does). Never had to use this myself!
c) When you just want to pass a new, clean EventArgs to the handler (probably because you don't think the person writing it can be trusted to check for null values. And you are probably right... :sigh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900