Null event stupidity?

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

    #1

    Null event stupidity?

    Why, oh why is it necessary to test an event for null before raising
    it?

    Why isn't that case handled automatically, instead of forcing
    developers to write three lines of wasted boilerplate code every time
    an event is raised:

    if (SomethingChang ed != null) // wasted code
    { // more waste
    SomethingChange d(...)
    } // more waste

    instead of simply:
    SomethingChange d(...)

    Either the C# compiler or the CLR should handle the null case
    automatically.
  • Michael Culley

    #2
    Re: Null event stupidity?

    "Kevin Cline" <kcline17@hotma il.com> wrote in message news:ba162549.0 311051806.1f9d4 c24@posting.goo gle.com...

    An event is just a delegate, so what you are suggesting is that delegates should be able to be called without being instantiated and
    the error ignored. In many cases you would want the error raised.

    --
    Michael Culley


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Null event stupidity?

      Kevin,
      The VB.NET compiler does the check for you automatically, via the RaiseEvent
      keyword.
      [color=blue]
      > Either the C# compiler or the CLR should handle the null case
      > automatically.[/color]
      I really do not think its the CLR's place to handle the null case on a
      delegate! As that would be "masking errors".
      [color=blue]
      > instead of simply:
      > SomethingChange d(...)[/color]
      Remember that "SomethingChang ed" is shorthand for
      "SomethingChang ed.Invoke()", if the CLR suddenly avoided Null Reference
      Exceptions when calling a method on a Null reference, should it do it in all
      cases or just in exceptional cases (no pun intended).

      Hope this helps
      Jay


      "Kevin Cline" <kcline17@hotma il.com> wrote in message
      news:ba162549.0 311051806.1f9d4 c24@posting.goo gle.com...[color=blue]
      > Why, oh why is it necessary to test an event for null before raising
      > it?
      >
      > Why isn't that case handled automatically, instead of forcing
      > developers to write three lines of wasted boilerplate code every time
      > an event is raised:
      >
      > if (SomethingChang ed != null) // wasted code
      > { // more waste
      > SomethingChange d(...)
      > } // more waste
      >
      > instead of simply:
      > SomethingChange d(...)
      >
      > Either the C# compiler or the CLR should handle the null case
      > automatically.[/color]


      Comment

      • Dave Benjamin

        #4
        Re: Null event stupidity?

        In article <ODoVrABpDHA.36 12@TK2MSFTNGP11 .phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:[color=blue][color=green]
        >> Either the C# compiler or the CLR should handle the null case
        >> automatically.[/color]
        >
        > I really do not think its the CLR's place to handle the null case on a
        > delegate! As that would be "masking errors".[/color]

        How do you figure? If there are no listeners, it's not an error. It's just a
        message with no recipients. This model forces the programmer to say "If
        anyone's listening, then say this to everyone that's listening. Otherwise,
        don't say anything". How is that different from saying "Say this to anyone
        that's listening"? If a tree falls in the woods....
        [color=blue][color=green]
        >> instead of simply:
        >> SomethingChange d(...)[/color]
        >
        > Remember that "SomethingChang ed" is shorthand for
        > "SomethingChang ed.Invoke()", if the CLR suddenly avoided Null Reference
        > Exceptions when calling a method on a Null reference, should it do it in all
        > cases or just in exceptional cases (no pun intended).[/color]

        But it's okay to use += or -= on a Null reference? You're not making any
        sense here...

        --
        ..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
        : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

        Comment

        • Daniel O'Connell

          #5
          Re: Null event stupidity?


          "Dave Benjamin" <ramen@lackingt alent.com> wrote in message
          news:slrnbqljfn .b74.ramen@lack ingtalent.com.. .[color=blue]
          > In article <ODoVrABpDHA.36 12@TK2MSFTNGP11 .phx.gbl>, Jay B. Harlow [MVP -[/color]
          Outlook] wrote:[color=blue][color=green][color=darkred]
          > >> Either the C# compiler or the CLR should handle the null case
          > >> automatically.[/color]
          > >
          > > I really do not think its the CLR's place to handle the null case on a
          > > delegate! As that would be "masking errors".[/color]
          >
          > How do you figure? If there are no listeners, it's not an error. It's just[/color]
          a[color=blue]
          > message with no recipients. This model forces the programmer to say "If
          > anyone's listening, then say this to everyone that's listening. Otherwise,
          > don't say anything". How is that different from saying "Say this to anyone
          > that's listening"? If a tree falls in the woods....[/color]

          An event is a field like any other when called. If its null its null, not an
          event with no listeners.
          While I agree that its annoying, there are other more important issues, like
          thread safety.
          [color=blue]
          >[color=green][color=darkred]
          > >> instead of simply:
          > >> SomethingChange d(...)[/color]
          > >
          > > Remember that "SomethingChang ed" is shorthand for
          > > "SomethingChang ed.Invoke()", if the CLR suddenly avoided Null Reference
          > > Exceptions when calling a method on a Null reference, should it do it in[/color][/color]
          all[color=blue][color=green]
          > > cases or just in exceptional cases (no pun intended).[/color]
          >
          > But it's okay to use += or -= on a Null reference? You're not making any
          > sense here...
          >[/color]

          += and -= eventually call Delegate.Combin e() and Delegate.Remove (), which
          can take null parameters. Remember, all operators are static, and a delegate
          modification creates a new delegate instead of modifying the original one.
          [color=blue]
          > --
          > .:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
          > : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :[/color]


          Comment

          • Michael Culley

            #6
            Re: Null event stupidity?

            "Dave Benjamin" <ramen@lackingt alent.com> wrote in message news:slrnbqljfn .b74.ramen@lack ingtalent.com.. .[color=blue]
            > How do you figure? If there are no listeners, it's not an error. It's just a
            > message with no recipients. This model forces the programmer to say "If
            > anyone's listening, then say this to everyone that's listening. Otherwise,
            > don't say anything". How is that different from saying "Say this to anyone
            > that's listening"? If a tree falls in the woods....[/color]

            But that would mean creating an instance of each event delegate for each object that is created.
            [color=blue]
            > But it's okay to use += or -= on a Null reference? You're not making any
            > sense here...[/color]

            Of course. += calls the static operator that takes two references as parameter and returns a reference, so is perfectly acceptable
            to work on a null reference:

            //not sure if this is syntatically correct but will be close
            public static operator MyClass +=(MyClass Ref1, MyClass Ref2)
            {
            if (Ref1 == null) Ref1 = new MyClass();
            .... do some sort of add here
            return Ref1
            }

            --
            Michael Culley


            Comment

            • Dave Benjamin

              #7
              Re: Null event stupidity?

              In article <#fiJq0NpDHA.25 88@tk2msftngp13 .phx.gbl>, Michael Culley wrote:[color=blue]
              > "Dave Benjamin" <ramen@lackingt alent.com> wrote in message news:slrnbqljfn .b74.ramen@lack ingtalent.com.. .[color=green]
              >> How do you figure? If there are no listeners, it's not an error. It's just a
              >> message with no recipients. This model forces the programmer to say "If
              >> anyone's listening, then say this to everyone that's listening. Otherwise,
              >> don't say anything". How is that different from saying "Say this to anyone
              >> that's listening"? If a tree falls in the woods....[/color]
              >
              > But that would mean creating an instance of each event delegate
              > for each object that is created.[/color]

              Well, it's all how you look at things. I didn't understand the null behavior
              very well, but I get it now (thanks to you and Daniel). But you could think
              of an event as a specialized collection object that contains only delegates,
              such that calling the collection as a function results in the delegates
              getting called. Obviously, that's not what's really going on here, but it
              would be an equally plausible solution to the multiple listener problem. In
              that case, you would just add/remove delegates to these collections as
              desired, and all events would be callable even if they were empty.

              Anyway, thanks for the insight.
              [color=blue][color=green]
              >> But it's okay to use += or -= on a Null reference? You're not making any
              >> sense here...[/color]
              >
              > Of course. += calls the static operator that takes two references as parameter and
              > returns a reference, so is perfectly acceptable to work on a null
              > reference... [snip][/color]

              Ahh, I hadn't realized that it worked that way. I guess that makes sense,
              but it's definitely a surprise to me. For instance, the following cannot hold:

              a.equals(b) <--> a == b

              Because null.equals(b) would be invalid. (Not sure if that's valid
              semantically in C# since I'm very new to the language).

              --
              ..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
              : d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

              Comment

              • Michael Culley

                #8
                Re: Null event stupidity?

                "Dave Benjamin" <ramen@lackingt alent.com> wrote in message news:slrnbqoh4t .sn1.ramen@lack ingtalent.com.. .[color=blue]
                > Well, it's all how you look at things. I didn't understand the null behavior
                > very well, but I get it now (thanks to you and Daniel). But you could think
                > of an event as a specialized collection object that contains only delegates,
                > such that calling the collection as a function results in the delegates
                > getting called. Obviously, that's not what's really going on here, but it
                > would be an equally plausible solution to the multiple listener problem. In
                > that case, you would just add/remove delegates to these collections as
                > desired, and all events would be callable even if they were empty.[/color]

                That would mean that each event would need to be created when the object was created. This would make instantiating the object much
                slower.
                [color=blue]
                > a.equals(b) <--> a == b[/color]

                The == operator is defined as

                public static bool operator ==(SomeObject A, SomeObject B)

                --
                Michael Culley


                Comment

                Working...