Tuesday, September 19, 2006

.NET Delegate & Event

@ A delegate is a class that can hold a reference to a method.
Unlike other classes, a delegate class has a signature, it can hold references only to methods that match its signature.

@ A delegate declaration is sufficient to define a delegate class. the declaration supplies the signature of the delegate, and the CLR provides the implementation.

@ EventHandler is predefined delegate class that specifically represents an event handler method for an event that does not return a value, whose 1st parameter is of type Object and refers to the instance that raises the event, and whose 2nd parameter is derived from type EventArgs and holds the event data.

@ An event is a message sent by an object to signal the occurence of an action.

@ In event communication, the event sender calss does not know which object or method will receive/handle the events it raises. What is needed is an intermediary(or pointer-like mechanism) between the sender and receiver. To associate an event with an event handler, add an instance of the delegate to the event.

@ How to wire up a Event Handler for an Event?
AddHandler OneObject.OneEvent, AddressOf OneEventHandler