473,769 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Sep 21 '06 #1
1 1621
Scott M. <No****@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 - <sk***@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
Sep 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3334
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It is possible to Add and Delete rows - Some cells have special attributes (readonly and events) Here's a snippet of the code:
3
2038
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When DataNode is changed, it raises "Changed" event. The class has a reference to the DataNode it is being contained in: - DataTree - tree like data structure that contains DataNodes; When
3
2210
by: nobs | last post by:
Hi guys I think I missunderstud something with delegates & events Hopefully someone can bring some light in this issue: Following problem I have a class which provide some events (like ServerQuitMsg) and in the code i call
6
2273
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When adding toolbar button: tbButton.Click += new...
7
3441
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the eventing code is slightly hidden. when you use events in Vb.Net you type this: <code> Public event MyEvent() </code>
2
2008
by: Mike | last post by:
Greetings, It would seem that this topic has been discussed at some length, but I was unable to discern whether there was a clear cut solution to resolve and/or otherwise workaround the issue. Briefly, I have some code which queries the database from a subform, and enables or disables a contol in the parent form, depending upon the results I receive.
0
2226
by: BigAl.NZ | last post by:
Hi Guys, I am trying to write/copy some code that uses events with a GPS. Everytime the GPS position updates the event fires. The GPS code is from a SDK Library that I got called GPS Tools from www.franson.com For some reason my code below doesnt work - the GpsFixEvent never seems to "fire" as it were.
1
2745
by: Christian Sengstock | last post by:
Hi all, i have a js application with several event handlers which i manage via prototype .bindAsEventListener. On normal page navigation ( link navigation ) i finish all events handlers (onUnload) if a new page should be loaded and everything works fine: All event handlers are finished and the new page works fine. However, if i do a browser "reload" the events a fired twice after it.
7
2344
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. Is that so?
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.