By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

about using eventArgs

P: n/a
Hello!

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 ?

//Tony
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jun 25, 3: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 ?
I'm afraid it's difficult to know exactly what you mean because you're
using terminology in a pretty loose manner. However, I'd suggest using
EventHandler<TEventArgsrather than creating your own delegate type
for every event. It makes life a lot more consistent in my view.

Jon
Jun 27 '08 #2

P: n/a
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.
Jun 27 '08 #3

P: n/a
On Jun 25, 10:29*am, "Tony" <johansson.anders...@telia.comwrote:
Hello!

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.
It's not very clear from your post the details of your situation.
If you have two events that need different set of arguments (even as
few as only one) then create two classes from EventArgs
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.