Create a custom means of managing the event invocation list : delegate Event « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Language Basics » delegate EventScreenshots 
Create a custom means of managing the event invocation list
Create a custom means of managing the event invocation list

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// Create a custom means of managing the event invocation list. 
   
using System;  
  
// Declare a delegate for an event.   
delegate void MyEventHandler();  
  
// Declare an event class that holds up to 3 events. 
class MyEvent {  
  MyEventHandler[] evnt = new MyEventHandler[3]
 
  public event MyEventHandler SomeEvent 
    // Add an event to the list. 
    add 
      int i; 
 
      for(i=0; i < 3; i++
        if(evnt[i== null) { 
          evnt[i= value;  
          break
        
      if (i == 3Console.WriteLine("Event list full.")
    
 
    // Remove an event from the list. 
    remove 
      int i; 
 
      for(i=0; i < 3; i++
        if(evnt[i== value) { 
          evnt[inull
          break
        
      if (i == 3Console.WriteLine("Event handler not found.")
    
  }  
  
  // This is called to fire the events.  
  public void OnSomeEvent() {  
      for(int i=0; i < 3; i++
        if(evnt[i!= nullevnt[i]();  
  }  
 
}  
  
// Create some classes that use MyEventHandler. 
class 
  public void Whandler() {  
    Console.WriteLine("Event received by W object");  
  }  
}  
  
class 
  public void Xhandler() {  
    Console.WriteLine("Event received by X object");  
  }  
}  
  
class {  
  public void Yhandler() {  
    Console.WriteLine("Event received by Y object");  
  }  
}  
  
class {  
  public void Zhandler() {  
    Console.WriteLine("Event received by Z object");  
  }  
}  
  
public class EventDemo4 {  
  public static void Main() {   
    MyEvent evt = new MyEvent();  
    W wOb = new W();  
    X xOb = new X();  
    Y yOb = new Y();  
    Z zOb = new Z()
 
    // Add handlers to the event list. 
    Console.WriteLine("Adding events.")
    evt.SomeEvent += new MyEventHandler(wOb.Whandler);  
    evt.SomeEvent += new MyEventHandler(xOb.Xhandler);  
    evt.SomeEvent += new MyEventHandler(yOb.Yhandler);  
 
    // Can't store this one -- full. 
    evt.SomeEvent += new MyEventHandler(zOb.Zhandler);  
    Console.WriteLine()
  
    // Fire the events.  
    evt.OnSomeEvent();  
    Console.WriteLine();  
  
    // Remove a handler. 
    Console.WriteLine("Remove xOb.Xhandler.")
    evt.SomeEvent -= new MyEventHandler(xOb.Xhandler);  
    evt.OnSomeEvent();  
 
    Console.WriteLine()
 
    // Try to remove it again. 
    Console.WriteLine("Try to remove xOb.Xhandler again.")
    evt.SomeEvent -= new MyEventHandler(xOb.Xhandler);  
    evt.OnSomeEvent();  
 
    Console.WriteLine()
 
    // Now, add Zhandler. 
    Console.WriteLine("Add zOb.Zhandler.")
    evt.SomeEvent += new MyEventHandler(zOb.Zhandler)
    evt.OnSomeEvent();  
 
  }  




           
       
Related examples in the same category
1.A very simple event demonstrationA very simple event demonstration
2.An event multicast demonstrationAn event multicast demonstration
3.Individual objects receive notifications when instance event handlers are usedIndividual objects receive notifications when instance     event handlers are used
4.A class receives the notification when a static method is used as an event handlerA class receives the notification when      a static method is used as an event handler
5.Use the bult-in EventHandler delegateUse the bult-in EventHandler delegate
6.Use delegate: eventUse delegate: event
7.Delegate and event hierarchyDelegate and event hierarchy
8.Events:Add and Remove FunctionsEvents:Add and Remove Functions
9.Events:Add and Remove Functions 2Events:Add and Remove Functions 2
10.Events: Custom Add and RemoveEvents: Custom Add and Remove
11.Delegates And EventsDelegates And Events
12.Delegates And Events 2Delegates And Events 2
13.Chaining events.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.