A .NET-compatible event : System Event : Services Event C# Source Code


Custom Search

C# Source Code » Services Event » System Event »

 

A .NET-compatible event









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// A .NET-compatible event. 
  
using System; 
 
// Derive a class from EventArgs. 
class MyEventArgs : EventArgs { 
  public int eventnum; 
} 
 
// Declare a delegate for an event.  
delegate void MyEventHandler(object source, MyEventArgs arg); 
 
// Declare an event class. 
class MyEvent { 
  static int count = 0; 
 
  public event MyEventHandler SomeEvent; 
 
  // This fires SomeEvent. 
  public void OnSomeEvent() { 
    MyEventArgs arg = new MyEventArgs(); 
 
    if(SomeEvent != null) { 
      arg.eventnum = count++; 
      SomeEvent(this, arg); 
    } 
  } 
} 
 
class X { 
  public void handler(object source, MyEventArgs arg) { 
    Console.WriteLine("Event " + arg.eventnum + 
                      " received by an X object."); 
    Console.WriteLine("Source is " + source); 
    Console.WriteLine(); 
  } 
} 
 
class Y {  
  public void handler(object source, MyEventArgs arg) { 
    Console.WriteLine("Event " + arg.eventnum + 
                      " received by a Y object."); 
    Console.WriteLine("Source is " + source); 
    Console.WriteLine(); 
  } 
} 
 
 
public class EventDemo5 { 
  public static void Main() {  
    X ob1 = new X(); 
    Y ob2 = new Y(); 
    MyEvent evt = new MyEvent(); 
 
    // Add handler() to the event list. 
    evt.SomeEvent += new MyEventHandler(ob1.handler); 
    evt.SomeEvent += new MyEventHandler(ob2.handler); 
 
    // Fire the event. 
    evt.OnSomeEvent(); 
    evt.OnSomeEvent(); 
  } 
} 



           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Services Event
» System Event