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
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement