a delegate is nothing more than a type safe method reference,
public delegate void NumberChangedEventHandler(int number)
This delegate can be used to call any method with a matching signature, the name of method and the name of parameter are not important, but you have to have the same signature.
Here is example:
public delegate void NumberChangedEventHander(int number);
public class MyPublisher
{
public NumberChangedEventHander NumberChanged;
}
public class MySubscriber : Page
{
public void OnNumberChanged(int number)
{
string mes = "New value is " + number ;
}
}
UI call the delegate:
MyPublisher publisher = new MyPublisher();
MySubscriber subscriber1 = new MySubscriber();
MySubscriber subscriber2 = new MySubscriber();
publisher.NumberChanged += new NumberChangedEventHander(subscriber1.OnNumberChanged);
publisher.NumberChanged += new NumberChangedEventHander(subscriber2.OnNumberChanged);
publisher.NumberChanged(3);
Tuesday, December 11, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment