For a static event, should sender be null?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news-server.san.rr.com

    For a static event, should sender be null?

    I have a class that defines a *static* event:

    | public static event FubarEventHandl er FubarAdded;


    This event is raised in public static methods, such as:


    | public static void AddFubar(...)
    | {
    | ...
    |
    | // Advise that a Fubar was added to the master Fubar list
    | if (FubarAdded != null)
    | FubarAdded(null , new FubarEventArgs( fubarObject));
    |
    | ...
    | }


    Note that the 'sender' parameter is null. In a static method, I don't have
    an instance, so I can't pass in 'this', and it seems silly for me to require
    the caller to pass in it's 'this' reference...

    I suppose I could pass in a reference to the static collection member that
    contains the list of Fubar objects, but that seems pretty meaningless.

    Is there a typical practice here? Is passing 'null' for sender OK in this
    situation?

    Thanks in advance.

    -Scott


  • Michael C

    #2
    Re: For a static event, should sender be null?

    "news-server.san.rr.c om" <scottseansmith 2@NO-SPAM.hotmail.co m> wrote in
    message news:t_Vie.1614 9$887.10001@tor nado.socal.rr.c om...[color=blue]
    > Is there a typical practice here? Is passing 'null' for sender OK in this
    > situation?[/color]

    Makes sense to me. It fits that the sender is null because there is no
    instance sending the event.

    Michael


    Comment

    • Bruce Wood

      #3
      Re: For a static event, should sender be null?

      The "sender" just forms part of the contract between the event source
      and the event handler. If the event source documents and defines the
      event such as "sender" is null, then it's up to the handler to
      understand that and not try to use it.

      So, yes, that's the right way to do it. Just document that fact where
      you declare the event (and, if you have a custom event delegate type,
      where you declare the delegate).

      Comment

      • Michael C

        #4
        Re: For a static event, should sender be null?

        "Bruce Wood" <brucewood@cana da.com> wrote in message
        news:1116531893 .920810.259230@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > The "sender" just forms part of the contract between the event source
        > and the event handler. If the event source documents and defines the
        > event such as "sender" is null, then it's up to the handler to
        > understand that and not try to use it.[/color]

        So you could really send anything through, eg clicking on a column in a grid
        kinda control could use the column as the sender?

        Michael


        Comment

        • Michael C

          #5
          Re: For a static event, should sender be null?

          "Bruce Wood" <brucewood@cana da.com> wrote in message
          news:1116531893 .920810.259230@ o13g2000cwo.goo glegroups.com.. .[color=blue]
          > The "sender" just forms part of the contract between the event source
          > and the event handler. If the event source documents and defines the
          > event such as "sender" is null, then it's up to the handler to
          > understand that and not try to use it.[/color]

          So you could really send anything through, eg clicking on a column in a grid
          kinda control could use the column as the sender?

          Michael


          Comment

          • Bruce Wood

            #6
            Re: For a static event, should sender be null?

            If you send an event from an object instance (like a column header in a
            grid), then it's good form to pass the object that raised the event as
            the "sender".

            However, if you're in Scott's situation and you're raising the event
            from a static method, and so you have no object instance handy, you
            might just as well pass null, so long as the event handlers know to
            expect that.

            Comment

            • Bruce Wood

              #7
              Re: For a static event, should sender be null?

              If you send an event from an object instance (like a column header in a
              grid), then it's good form to pass the object that raised the event as
              the "sender".

              However, if you're in Scott's situation and you're raising the event
              from a static method, and so you have no object instance handy, you
              might just as well pass null, so long as the event handlers know to
              expect that.

              Comment

              • Michael C

                #8
                Re: For a static event, should sender be null?

                "Bruce Wood" <brucewood@cana da.com> wrote in message
                news:1116544351 .804552.127270@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                > If you send an event from an object instance (like a column header in a
                > grid), then it's good form to pass the object that raised the event as
                > the "sender".
                >
                > However, if you're in Scott's situation and you're raising the event
                > from a static method, and so you have no object instance handy, you
                > might just as well pass null, so long as the event handlers know to
                > expect that.[/color]

                The programmer using the event will work it out soon enough :-)

                Michael


                Comment

                • Michael C

                  #9
                  Re: For a static event, should sender be null?

                  "Bruce Wood" <brucewood@cana da.com> wrote in message
                  news:1116544351 .804552.127270@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                  > If you send an event from an object instance (like a column header in a
                  > grid), then it's good form to pass the object that raised the event as
                  > the "sender".
                  >
                  > However, if you're in Scott's situation and you're raising the event
                  > from a static method, and so you have no object instance handy, you
                  > might just as well pass null, so long as the event handlers know to
                  > expect that.[/color]

                  The programmer using the event will work it out soon enough :-)

                  Michael


                  Comment

                  Working...