SimpleDelegateFunction called from Ob1, string=Event fired! Event fired!(Ob1): 3:49:46 PM on Friday, May 10, 2002 Event fired!(Ob1): 1056318417 SimpleDelegateFunction called from Ob2, string=Event fired! Event fired!(Ob2): 3:49:46 PM on Friday, May 10, 2002 Event fired!(Ob2): 1056318417 |
namespace DelegatesAndEvents { class DelegatesAndEvents { public delegate void PrintString(string s); public event PrintString MyPrintString; public void FirePrintString(string s) { if (MyPrintString != null)MyPrintString(s); } } class TestDelegatesAndEvents { [STAThread] static void Main(string[] args) { DelegatesAndEvents dae =new DelegatesAndEvents(); MyDelegates d = new MyDelegates(); d.Name = "Ob1"; dae.MyPrintString +=new DelegatesAndEvents.PrintString(d.SimpleDelegateFunction); // ... more code similar to the // above few lines ... dae.FirePrintString("Event fired!"); } } class MyDelegates { // ... "Name" property omitted... public void SimpleDelegateFunction(string s) { Console.WriteLine("SimpleDelegateFunction called from {0}, string={1}", m_name, s); } // ... more methods ... } } |