EventHandlers syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.microsoft.com

    #1

    EventHandlers syntax

    Hi,

    Why was it decided to implement subscribing an observer to an event with
    += and -= operators and not providing some kind of .Add and .Remove methods?
    Wouldnt this be nicer?

    having it as something.Event Here += new BlahHandler(cla ss.method):

    instead of

    something.Event Here.Add(class. method); for +=

    and

    something.Event Here.Remove(cla ss.Method)

    what other way are we going to add a callback method?

    = ? then why not use singlecast delegates. we could always provide a
    ..clear() method

    Just a thought


  • Marc Scheuner [MVP ADSI]

    #2
    Re: EventHandlers syntax

    > Why was it decided to implement subscribing an observer to an event with[color=blue]
    >+= and -= operators and not providing some kind of .Add and .Remove methods?
    >Wouldnt this be nicer?
    >
    > having it as something.Event Here += new BlahHandler(cla ss.method):
    >
    >instead of
    > something.Event Here.Add(class. method); for +=[/color]

    Well, I guess this really boils down to a personal preference - one
    could argue MS should have provided both ways, especially seeing that
    in VB.NET, you do have the AddHandler method.

    But really, in the end, functionally you're not getting any additional
    power by having an Add method - so just get used to the way it is now
    and get on to more important problems ;-)

    Marc
    =============== =============== =============== =============== ====
    Marc Scheuner May The Source Be With You!
    Bern, Switzerland m.scheuner(at)i nova.ch

    Comment

    • news.microsoft.com

      #3
      Re: EventHandlers syntax

      Well i mean its just a COLLECTION (List) of delegates to invoke (for a
      Multicast delegate) and collection classes have Add and Remove methods so
      why not provide the same for the delegate invokation collection? They do
      have .Combine methods, just a thought.

      I havnt tried it but if we call += on a SingleCast delegate do we get an
      error? Depends how one views the += operator, is it changed to a = or is it
      treated as an error? Would the compiler be that strict? Havnt tried it yet
      just a curiosity.




      "Marc Scheuner [MVP ADSI]" <m.scheuner@ino va.SPAMBEGONE.c h> wrote in message
      news:2tsupvkdoq gubtd6fnlt1utej rj3p0bcif@4ax.c om...[color=blue][color=green]
      > > Why was it decided to implement subscribing an observer to an event[/color][/color]
      with[color=blue][color=green]
      > >+= and -= operators and not providing some kind of .Add and .Remove[/color][/color]
      methods?[color=blue][color=green]
      > >Wouldnt this be nicer?
      > >
      > > having it as something.Event Here += new BlahHandler(cla ss.method):
      > >
      > >instead of
      > > something.Event Here.Add(class. method); for +=[/color]
      >
      > Well, I guess this really boils down to a personal preference - one
      > could argue MS should have provided both ways, especially seeing that
      > in VB.NET, you do have the AddHandler method.
      >
      > But really, in the end, functionally you're not getting any additional
      > power by having an Add method - so just get used to the way it is now
      > and get on to more important problems ;-)
      >
      > Marc
      > =============== =============== =============== =============== ====
      > Marc Scheuner May The Source Be With You!
      > Bern, Switzerland m.scheuner(at)i nova.ch[/color]


      Comment

      Working...