C# Events & Event Handlers

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

    C# Events & Event Handlers

    I'm trying to get my head around what's involved in C# to declare, raise and
    respond to an event.

    I'm coming from the VB.NET world where to declare and event we wrote:

    Public Event Foo()

    To raise the event, we wrote:

    RaiseEvent Foo()

    To create an instance that could respond to events, we wrote:

    WithEvents x As SomeTypeThatHas TheFooEvent

    And finally, we wrote the event handler as a regular method, but with a
    "handles" clause:

    Sub Foo() Handles x.Foo()

    Can someone please explain the C# for this VB.NET code? I've looked into it
    and I'm getting code like this, but I don't know exactly where it should go
    and what it does:

    // The following is like the VB.NET Public Event Foo() declaration, right?
    public event FooEventHandler Foo;

    // I have no idea what this does!
    public delegate void FooEventHandler ();

    //The following raises the event?
    Foo();

    // Now, in the code that will use this class:
    // I have no idea what this is all about!
    x.Foo += new System.EventHan dler(x_Foo);

    //Then, to handle the event in the code that uses the class:
    private static void myCar_SpeedLimi tExceeded()
    {
    // do something to handle the event here;
    }

    I have everything where I believe it should be, but I am getting a build
    error on this line:

    x.Foo += new System.EventHan dler(x_Foo);

    The error indicates that I can't implicitly convert a System.EventHan dler to
    a FooEventHandler

    -Thanks!


  • Jon Skeet [C# MVP]

    #2
    Re: C# Events & Event Handlers

    Scott M. <NoSpam@NoSpam. comwrote:
    I'm trying to get my head around what's involved in C# to declare, raise and
    respond to an event.
    See http://www.pobox.com/~skeet/csharp/events.html for fairly full
    coverage of what C# does. It explains the difference between events,
    delegate types, and delegate instances - it's vital that you understand
    that in order to see what's going on.

    The tricky bit (for me) is working out exactly what VB.NET is doing...
    I'm coming from the VB.NET world where to declare and event we wrote:
    >
    Public Event Foo()
    Using Reflector, it looks like this is creating a delegate type *and*
    creating an event in the declaring type, *and* creating a delegate
    instance variable in the declaring type to back the event. In other
    words, it's roughly the equivalent of

    public delegate void FooHandler(); // Type declaration
    public event FooHandler Foo; // Field-like event declaration
    To raise the event, we wrote:
    >
    RaiseEvent Foo()
    In this case, you'd just call Foo() - for non-field-like events in C#,
    you'd need to call whatever delegate instance is backing the event
    (assuming you have one).
    To create an instance that could respond to events, we wrote:
    >
    WithEvents x As SomeTypeThatHas TheFooEvent
    >
    And finally, we wrote the event handler as a regular method, but with a
    "handles" clause:
    >
    Sub Foo() Handles x.Foo()
    There's no direct equivalent of these in C#. Instead, you need to
    "manually" subscribe and unsubscribe from events at the appropriate
    times.

    Hopefully the article referenced above will answer the rest of your
    questions.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    Working...