event accessors

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

    #1

    event accessors

    When I try to define accessors for this event, the event invocation
    line stops compiling. What's wrong?

    public class TestClass
    {
    public delegate int MyDelegate(stri ng s);
    public event MyDelegate myEvent
    {
    add { Console.WriteLi ne("myEvent.Add "); }
    remove { Console.WriteLi ne("myEvent.Rem ove"); }
    }
    public void Foo(object sender, System.EventArg s e)
    {
    myEvent("event" ); // Compiler error: The event can only
    // appear on the left hand side of += or -=
    }
    }
  • Mattias Sjögren

    #2
    Re: event accessors

    Brad,
    [color=blue]
    >When I try to define accessors for this event, the event invocation
    >line stops compiling. What's wrong?[/color]

    By using the explicit accessor syntax, you no longer get the
    underlying delegate auto generated by the compiler. So you have to add
    a MyDelegate field yourself and add/remove handlers to it inside the
    add and remove accessors.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    Working...