Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Difference between delegates and events
Message
De
06/09/2002 11:37:20
 
 
À
05/09/2002 04:28:14
Martin Van Krieken
Capgemini Nederland Bv
Utrecht, Pays-Bas
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00696983
Message ID:
00697560
Vues:
11
Hi Martin,

I'll give this a stab (still fairly new to this), hopefully someone will correct me if I misspeak.

A delegate is a type safe function pointer. It allows the passing of a reference to a method entry point w/o explicitly calling the method. Think callback.

An event is a notification raised by a control (or object) when some criteria is met.

An event uses a delegate to call an EventHandler method.

In C# you would implement this by
declaring a delegate
declaring an event of the delegate type
create an instance of the delegate, passing is the name of the eventhandler method.

hookup the event with the eventhandler

implementing your method that will be the event handler - the method has the same signature as the delegate.

C# syntax

public delegate void myDelegate(int X);
public event myDelegate MyEvent;
MyClass.MyEvent += new MyDelegate(MyEventHandler);

private void MyEventHandler(int X)
{
}


I believe VB.Net hides alot of this complexity from you. It use the Handles keyword to hook up the eventhandler. It also has AddHandler to specify and event and AddressOf to create the pointer.

HTH,
Bill


>Hello,
>
>I'm busy getting over the learning curve of .NET and C#. I'm reading a book of Andrew Troelsen (C# and the .NET platform). I'm not familliar with delegates and defining my own events (I have a VFP background). I'm sure the concept of delegates and events is very owerfull, but I don't understand the difference between the two (apart from the keyword 'event'). Kan anyone explain this?
>
>TIA,
>
>Martin
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform