Why event handler first parameters are incorrect

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

    Why event handler first parameters are incorrect

    I noticed that DataGridView CellValidated() and other event handler first
    parameter is object:

    Grid.CellValida ted+=new
    DataGridViewCel lEventHandler(G rid_CellValidat ed);
    ....

    void Grid_CellValida ted(object sender, DataGridViewCel lEventArgs e)
    {
    .....
    }

    Why ? Correct signature must be

    void Grid_CellValida ted(DataGridVie w sender, DataGridViewCel lEventArgs e) {

    Andrus.

  • jj

    #2
    Re: Why event handler first parameters are incorrect

    Hello Andrus

    I do not have an answer to your question. But I'm curious: what is the
    problem if sender is of the type System.Object? We both know that we
    can easily cast.....maybe you want to avoid this casting?

    Regards,
    Jim

    Comment

    • =?windows-1257?Q?Lasse_V=E5gs=BFther_Karlsen?=

      #3
      Re: Why event handler first parameters are incorrect

      Andrus wrote:
      I noticed that DataGridView CellValidated() and other event handler
      first parameter is object:
      >
      Grid.CellValida ted+=new
      DataGridViewCel lEventHandler(G rid_CellValidat ed);
      ...
      >
      void Grid_CellValida ted(object sender, DataGridViewCel lEventArgs
      e) {
      ....
      }
      >
      Why ? Correct signature must be
      >
      void Grid_CellValida ted(DataGridVie w sender, DataGridViewCel lEventArgs e) {
      >
      Andrus.
      Event handler delegates follow a standard that says that:

      1. argument #1 must be of type Object
      2. argument #2 must descend from EventArgs

      While the actual object being sent to your event handler implementation
      is a DataGridView, the sender parameter still needs to be declared as
      type Object.

      --
      Lasse Vågs¿ther Karlsen
      mailto:lasse@vk arlsen.no
      Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

      PGP KeyID: 0xBCDEA2E3

      Comment

      • Andrus

        #4
        Re: Why event handler first parameters are incorrect

        I do not have an answer to your question. But I'm curious: what is the
        problem if sender is of the type System.Object? We both know that we
        can easily cast.....maybe you want to avoid this casting?
        I have 2 issues with this:

        1. It requires to use casting. Good coding style requires not to use casts.

        2. I must create my own entity property events invoked from DataGridView
        events if column is changed.

        Is it OK to pass DataGridView as sender parameter and event arguments in
        command line, without using Eventargs class ?
        In some cases I do'nt need sender. Is it OK to create event without sender
        parameter ?

        Andrus.

        Comment

        • Andrus

          #5
          Re: Why event handler first parameters are incorrect

          Lasse,
          Event handler delegates follow a standard that says that:
          >
          1. argument #1 must be of type Object
          2. argument #2 must descend from EventArgs
          >
          While the actual object being sent to your event handler implementation
          is a DataGridView, the sender parameter still needs to be declared as
          type Object.
          Where to find link to this standard ?
          I havent seen any reference to it.
          Why this standard exists ? I must use ugly casts due to this.
          >I can only speak to what I've observed, and the reason for this is that it
          >is easier to re-use an event handler implementation for various controls,
          >since most of the difference is in the EventArgs-descendant anyway.
          I'm planning to create my own event which are set by DataGridView if entity
          property is changed and committed.
          Is it OK to create event wothout sender and passing aruments as parameters,
          without using EventArgs class ?
          >I've seen it in an older video with Anders Hejlsberg where he talked about
          >the reasoning behind using EventArgs, and why Sender was type-less so to
          >speak, but I can't remember the actual link.
          Only information I have found is from Jon's website.Jon does'nt describe it.
          >And yes, you'll have to use a typecast. It's just the way it is so you'd
          >just better get used to it.
          Andrus.

          Comment

          • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

            #6
            Re: Why event handler first parameters are incorrect

            Andrus wrote:
            >I do not have an answer to your question. But I'm curious: what is the
            >problem if sender is of the type System.Object? We both know that we
            >can easily cast.....maybe you want to avoid this casting?
            >
            I have 2 issues with this:
            >
            1. It requires to use casting. Good coding style requires not to use casts.
            >
            2. I must create my own entity property events invoked from DataGridView
            events if column is changed.
            >
            Is it OK to pass DataGridView as sender parameter and event arguments in
            command line, without using Eventargs class ?
            In some cases I do'nt need sender. Is it OK to create event without
            sender parameter ?
            >
            Andrus.
            You can write event handler declarations with all sorts of arguments.
            The Object/EventArgs standard is not enforced by the compiler.

            The EventArgs part is for versioning though, easier to add more
            properties to this class than it is to fix all the places where you need
            to add more arguments later.

            Also remember that if at some point you suddenly want sender, it will
            require you to go back and fix all places where you use these events.
            Why not just add the sender now and just ignore it?

            --
            Lasse Vågsæther Karlsen
            mailto:lasse@vk arlsen.no
            Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

            PGP KeyID: 0xBCDEA2E3

            Comment

            • Peter Duniho

              #7
              Re: Why event handler first parameters are incorrect

              On Mon, 26 May 2008 05:54:57 -0700, Andrus <kobruleht2@hot .eewrote:
              Where to find link to this standard ?

              I havent seen any reference to it.
              There is a prominent link to the above page from the main "Events (C#
              Programming Guide)" page.
              Why this standard exists ? I must use ugly casts due to this.
              "Ugly" is in the eye of the beholder. Also, I doubt anyone here can tell
              you precisely why the standard exists. But we can offer some possible
              benefits as suggestions for why it might be that way:

              * Avoids type proliferation. There's a delegate type for every event
              signature. We already have a different delegate type for every different
              event data argument type (EventArgs-derived classes). You can imagine the
              number of types that would be required to support every combination of
              possible sender and event data arguments.

              * Typing the sender more narrowly than Object doesn't necessarily
              remove the need to cast anyway. In the Forms namespace, for example, it's
              not uncommon to have a single handler deal with events from multiple
              objects of different types. Inasmuch as you need the actual type of the
              sender (not always the case, but does happen), you'd wind up casting from
              the given type anyway (if a "lowest-common-denominator" type was used,
              like Control), or you would not be able to easily use the same handler
              with multiple events (if the event was always declared to match the actual
              type of the sender).

              There may be other reasons, but IMHO the first of the above is quite
              significant, and the second of the above effectively minimizes your claim
              that one could avoid "ugly casts" in a practical way without sacrificing
              existing benefits.
              I'm planning to create my own event which are set by DataGridView if
              entity property is changed and committed.
              Is it OK to create event wothout sender and passing aruments as
              parameters, without using EventArgs class ?
              It depends on how you're going to use it. But yes, assuming your code is
              the only code that will ever use the event, you can make it look any way
              you like. Some binding mechanisms rely on your code following the .NET
              convention, so if you care about your event and associated property being
              useful in a binding scenario (not uncommon for a "property changed"
              event), you should follow the .NET design standard.

              Pete

              Comment

              • harborsparrow

                #8
                Re: Why event handler first parameters are incorrect

                The original question was about the signature of this statement:

                Grid.CellValida ted += new
                DataGridViewCel lEventHandler(G rid_CellValidat ed);

                In the above statement, "DataGridViewCe llEventHandler" is a delegate
                whose sole purpose is to pass the NAME (i.e., really an address) of an
                event handler to the list of registered methods to be called should
                that event occur in the future. The above statement just adds the
                address of "Grid_CellValid ated" to the linked list of procedures which
                will be called in the future when the "CellValida ted" event gets
                raised. Or in Java parlance, the statement above registers
                "Grid_CellValid ated" as a listener.

                "Grid_CellValid ated" is the actual event handler, and as such, it must
                have the event handler signature (sender, Eventargs).


                ....



                Comment

                Working...