EventArgs pattern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Cadwell

    #1

    EventArgs pattern

    What advantages do I reap by following the EventArgs pattern in defining
    custom event delegates?

    For example...

    public delegate void CustomEventHand ler(object sender, CustomEventArgs e);
    public class CustomEventArgs : System.EventArg s
    {
    int index = 0;
    public CustomEventArgs (int Index)
    {
    index = Index;
    }
    public int Index
    {
    get { return index; }
    }
    }

    ....instead of simply using...

    public delegate void CustomEventHand ler(int Index);


    It's so much more work. There's a lot of boilerplate in writing events and
    controls.

    Thanks,
    Eric



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: EventArgs pattern

    Eric,

    The overall reason is that it is quite simply the pattern that was
    established. While this doesn't sound like the best reason in the world, it
    is the way it is, and it makes everything easier because its implementation
    is just short of universal.

    However, I can say that there is one benefit to this pattern. Because a
    method can be registered to handle multiple event notifications, the sender
    parameter becomes very, very important. It allows for more reusable code.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Eric Cadwell" <ecadwell@ns.in sight.com> wrote in message
    news:u8f4b0voDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
    > What advantages do I reap by following the EventArgs pattern in defining
    > custom event delegates?
    >
    > For example...
    >
    > public delegate void CustomEventHand ler(object sender, CustomEventArgs e);
    > public class CustomEventArgs : System.EventArg s
    > {
    > int index = 0;
    > public CustomEventArgs (int Index)
    > {
    > index = Index;
    > }
    > public int Index
    > {
    > get { return index; }
    > }
    > }
    >
    > ...instead of simply using...
    >
    > public delegate void CustomEventHand ler(int Index);
    >
    >
    > It's so much more work. There's a lot of boilerplate in writing events and
    > controls.
    >
    > Thanks,
    > Eric
    >
    >
    >[/color]


    Comment

    • Guest's Avatar

      #3
      Re: EventArgs pattern

      Because then we have a uniform way to specify events.

      Btw you should have youre field set to readonly unless you are doing
      something like cancellable events then its writable

      If you dont need custom event args you can just use the EventArgs base.

      Why dont you just make a template of the event or a wizard to generate the
      ..cs file then its a simple matter of right click -> Add new Event (just like
      add new class) in the IDE context menu (theres an idea :D how bout that in
      the next IDE?)




      "Eric Cadwell" <ecadwell@ns.in sight.com> wrote in message
      news:u8f4b0voDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
      > What advantages do I reap by following the EventArgs pattern in defining
      > custom event delegates?
      >
      > For example...
      >
      > public delegate void CustomEventHand ler(object sender, CustomEventArgs e);
      > public class CustomEventArgs : System.EventArg s
      > {
      > int index = 0;
      > public CustomEventArgs (int Index)
      > {
      > index = Index;
      > }
      > public int Index
      > {
      > get { return index; }
      > }
      > }
      >
      > ...instead of simply using...
      >
      > public delegate void CustomEventHand ler(int Index);
      >
      >
      > It's so much more work. There's a lot of boilerplate in writing events and
      > controls.
      >
      > Thanks,
      > Eric
      >
      >
      >[/color]


      Comment

      • Eric Gunnerson [MS]

        #4
        Re: EventArgs pattern

        It is at times helpful to have a standard pattern - especially if you're
        building a framework - but I wouldn't view it as a requirement.

        --
        Eric Gunnerson

        Visit the C# product team at http://www.csharp.net
        Eric's blog is at http://blogs.gotdotnet.com/ericgu/

        This posting is provided "AS IS" with no warranties, and confers no rights.
        "Eric Cadwell" <ecadwell@ns.in sight.com> wrote in message
        news:u8f4b0voDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
        > What advantages do I reap by following the EventArgs pattern in defining
        > custom event delegates?
        >
        > For example...
        >
        > public delegate void CustomEventHand ler(object sender, CustomEventArgs e);
        > public class CustomEventArgs : System.EventArg s
        > {
        > int index = 0;
        > public CustomEventArgs (int Index)
        > {
        > index = Index;
        > }
        > public int Index
        > {
        > get { return index; }
        > }
        > }
        >
        > ...instead of simply using...
        >
        > public delegate void CustomEventHand ler(int Index);
        >
        >
        > It's so much more work. There's a lot of boilerplate in writing events and
        > controls.
        >
        > Thanks,
        > Eric
        >
        >
        >[/color]


        Comment

        • Eric Cadwell

          #5
          Re: EventArgs pattern

          Thanks all.

          -Eric


          Comment

          Working...