467,883 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,883 developers. It's quick & easy.

about using eventArgs

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
  • viewed: 1766
Share:
3 Replies
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
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
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.

Similar topics

1 post views Thread by e-mid | last post: by
11 posts views Thread by Brian | last post: by
12 posts views Thread by Mahesh Devjibhai Dhola [MVP] | last post: by
3 posts views Thread by Tony | last post: by
reply views Thread by Tony Johansson | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.