On Jun 25, 6:29*pm, "Tony" <johansson.anders...@telia.comwrote:
One more thing assume I create an event and an object derived from the
eventArgs containing some info about the event.
Then create another event also with an object derived from the eventArgs
containing some info about the
event.
When creating the event handler I have two choices either use EventArgs as
the second parametr and cast to the actual type
or
use two separate event handler with the actual type as the second paramer..
So does it exist some recommended guidline to which one to use.
I would say use different event handler unles they they are logically
connected in some way.
Is it possible to say anything about which of these two alternatives is the
best choice ?
If you are subscribing to two events that are so different in nature
that they use two distinct EventArgs-derived classes, then it is
doubtful that you could get away with one event handler for both.
About the only case where this is true is when your event handler does
not use the EventArgs argument at all - in this case, it is probably
better to write a single handler which just takes EventArgs.
If your EventArgs classes have several properties in common (and not
just names, but actual meanings of those properties), then you might
want to introduce an abstract base class in your hierarchy covering
those properties. Then, event handler can be declared with an argument
of that class and reused, while still having access to all those
common properties.