Results 1 to 2 of 2

Thread: Event Parameter Questions

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    South Louisiana

    Event Parameter Questions

    I've been creating custom classes for a while now, but today I started with a new class and decided to use the XML comments. What I've done in the past is send two parameters: sender and e. The sender is an Object type and the e is an EventArgs type.

    My question is two-fold. First, should I really keep sender as an Object or should I change it to the class that I'm creating? Secondly, is EventArgs needed whenever I just pass EventArgs.Empty as the value?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002

    Re: Event Parameter Questions

    The answer to the first question is yes, probably. You certainly could type sender to whatever type is raising the event, but that would mean that any handler would have to know that and get it right (or else you wouldn't be able to declare the event handler). That is certainly a pretty low bar for users of the class to get over, but how often have you written an event handler, and in all those event handlers, what percentage made use of sender? You have probably written thousands of event handlers (counting double clicking on a button, and similar creation of event handlers), and probably make use of the sender object no more than about 1% of the time. So, you will be requiring people to figure out the type when they usually don't care about the argument at all.

    Another point is that you can't very well share the events between objects if sender isn't an object. This may not matter for a custom event, as you may not ever expect any other class to want to make use of the event, and if they did, you could make a different event for them without much effort.

    So, neither argument states that you must not make sender type Object, but both arguments suggest that it is somewhat better if you do, so what argument can be made to say that you shouldn't? The only possible reason is that you know that you WILL be making use of sender and want to skip the DirectCast.

    As for EventArgs, that is a convention. I seem to remember that you have to have that to meet some pattern, but I forget what the pattern is. I remember that JMC wrote about it a year or so back (I'm not going looking for it), I just forget the details.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width