Confussed about parameters for Events

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

In one of my books called "Mastering C#" there is a statement that reads
"All event handler delegates must return void and accept two parameters. The
first parameter is an object, and it represents the object that raises the
event... The second is a parameter that is an object of a class derived from
the System.EventArgs class".

Now I know that this can not be true in general because I have created
events that do not match these parameters. I know there are some system
delegates that are designed to be used with event handers that are defined
with this signiture and using one of those would require the appropriate
parameters to be passed to it.

I just though maybe I was missing something and thought if anyone can
clarify things, that would be great!.

Thanks for your help

Earl
 
The book is either in error or out of context...

(almost?) all of the event handler delegates in the FCL use this signature,
but it is not a requirement -- rather it is a guideline.

public delegate void SomeEventHandler( object sender,
MyClassThatInheritsFromEventArgs e);
....
public event SomeEventHandler SomeEvent;

This is the preferred pattern for events. However, it's not required. Just
as valid would be
public delegate int SomeEventHandler( int x, int y);
....
public event SomeEventHandler SomeEvent;

Now, ignoring statndards and best practices is done at your own risk, but
there's no technical reason you can't do the last one.

The nice thing about using the first example is that if someone didn't care
for the extended information in the second parameter, they could use
System.EventHandler delegates and hook up to your events blindly. But
again, not a requirement, just a strong guideline.
 
Philip, thanks for the reply. I read the entire chapter so I don't think I
got anything out of context. Overall Mastering C# is a very good and easy to
understand book but I guess the author missed on this one. No more
confussion on my part. Thanks

Earl
 
Jay,

I have normally used these conventions because the books I have read have
done them this way, but it is great to have the official recommedations...
Thanks...

Earl
 
Back
Top