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 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!


.



Relevant Pages

  • Re: event handler
    ... It goes exactly as you tell, therefore you have to create a public event in ... raise that events in form 2 when happens what you tell ... declare your form2 Friend withevents in form1 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: raising Page event with customs webcontrol
    ... if you declare a public event on the control (e.g raise it from the ... Public Event MyEvent As EventHandler ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: How to implement sizeof operator
    ... project i want to implemnent my own sizeof operator function (like ... Hint: macros can declare variables. ... difference between foo and foo, where the first foo was declared using ... No, at compile-time, it can be done in pure ISO C. ...
    (comp.lang.c)
  • Re: Subroutines with &
    ... print foo(); ... In addition to the expected "Howdy", it produces the following warning: ... You could declare the sub above the ... Or, you could forward declare the sub, like this: ...
    (comp.lang.perl.misc)
  • Re: Calling functions declared in an entity
    ... In VHDL it is possible to declare the following entity: ... entity Foo is ... ARCHITECTURE rtl OF ent_item_decl IS ...
    (comp.lang.vhdl)