C# Events & Event Handlers

S

Scott M.

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 SomeTypeThatHasTheFooEvent

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.EventHandler(x_Foo);

//Then, to handle the event in the code that uses the class:
private static void myCar_SpeedLimitExceeded()
{
// 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.EventHandler(x_Foo);

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

-Thanks!
 
J

Jon Skeet [C# MVP]

Scott M. said:
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 SomeTypeThatHasTheFooEvent

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top