I've read that one benefit to the MyEventHandler
/MyEventArgs
model is that it allows standard event handlers to handle a variety of events. It sounds good, but perhaps I'm understanding how this is supposed to work. I have the following code:
public delegate void DataArrivalEventHandler
(object sender, DataArrivalEventArgs e);
public class DataArrivalEventArgs : EventArgs
{
public DateTime Arrival { get; protected set; }
public DataArrivalEventArgs()
{
Arrival = DateTime.Now;
}
public DataArrivalEventArgs(DateTime arrival)
{
Arrival = arrival;
}
}
...
_pipeReader.DataArrival += new EventHandler(Pipe_DataArrival);
...
private void Pipe_DataArrival(object sender, EventArgs e)
{
...
}
The code throws an error when I'm trying to add the event handler, however, saying that it cannot implicity cast DataArrivalEventHandler
to EventHandler
. Changing DataArrivalEventHandler(Pipe_DataArrival)
to EventHandler(Pipe_DataArrival)
fixes the problem, so I feel like you should be able to add generic event handlers to more specific events (I understand why you can't do it the other way around.)
Is how I have it the best way to do it, or is there a better convention?