[RESOLVED] [2.0] Delegates and Events
OK, having mucked around with VB.NET for a while, I have decided to have a muck around with C# (2005 Express). So far not too bad, getting used to the syntax etc, but I am currently banging my head against the brick wall called 'Delegates and Events'.
Have read articles on MSDN and elsewhere my head is spinning a bit.
As far as I can work out, a delegate is some sort of class or container that can contain references to methods. (Yes?)
So, when you 'wire-up' an event, you pass in the method you want to call when the event fires like so:
Code:
obj.MyEvent += new MyEventHandler(MyMethod); // where MyEventHandler is the delegate
Is the '+=' basically adding MyMethod to a list of methods that is automatically called when the event fires?
Am I close, or miles away with my take on this? :confused:
Thanks for helping out a C# newbie!
Re: [2.0] Delegates and Events
It sounds to me like you got it. :)
Are you familiar with doing this in VB?
vb.net Code:
AddHandler Button1.Click, AddressOf DoSomething
If so then you already know about this:
c# Code:
Button1.Click += new EventHandler(DoSomething);
Re: [2.0] Delegates and Events
A delegate is basically as you described: an object that contains a reference to a method. When you invoke the delegate you are executing the method that it references. Normally you would need a reference to the object of which the method was a member in order to execute it. The delegate allows you to pass a method reference around as an object without having to pass the object of which the method is a member. This will become clearer with an example below.
An event is basically a collection of EventHandler delegates. By using the "+=" operator you are adding a delegate to the collection. When an object raises an event what it is doing is basically looping through the collection of delegates for that event and invoking each one. In that way each method that is registered to handle that event will be executed when the event is raised.
Now, consider the situation where you have a form and a button. You declare a method on the form and register it as an event handler for the button's Click event. When the user clicks the button it raises its Click event, thus invoking the EventHandler delegate and executing the method. The button has thus executed a method that is a member of a form that it has no reference to.
All this happens in VB too but the complexity is hidden behind the Handles, AddHandler and RaiseEvent statements.
Re: [2.0] Delegates and Events
Quote:
Originally Posted by nmadd
Are you familiar with doing this in VB?
Code:
AddHandler Button1.Click, AddressOf DoSomething
Yes, I am familiar with AddHandler and AddressOf in VB, but I guess I never thought too deeply about it because I didn't have too. VB hides a lot of stuff away from you, and 'it just works'. When you lay out the two code snippets for VB and C#, it is obvous how similar they are.
Quote:
Originally Posted by jmcilhinney
An event is basically a collection of EventHandler delegates. By using the "+=" operator you are adding a delegate to the collection.
Thanks for this explanation, it has helped me a lot by making me think of an event as a collection, rather than just one thing. :thumb:
When I come to think about it, it does make more sense. A class can raise an event, and a number of methods can be 'wired' to it, so it stands to reason these methods must be stored or listed somewhere, ready to be called when the event fires.
Thanks, I think there is a bit of light at the end of the tunnel. :)