Individual objects receive notifications when instance event handlers are used : 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 
Individual objects receive notifications when instance event handlers are used
Individual objects receive notifications when instance     event handlers are used

/*
C#: The Complete Reference 
by Herbert Schildt 

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


/* Individual objects receive notifications when instance 
   event handlers are used. */ 
  
using System; 
 
// Declare a delegate for an event.  
delegate void MyEventHandler()
 
// Declare an event class. 
class MyEvent 
  public event MyEventHandler SomeEvent; 
 
  // This is called to fire the event. 
  public void OnSomeEvent() { 
    if(SomeEvent != null
      SomeEvent()
  

 
class 
  int id; 
 
  public X(int x) { id = x; 
 
  // This is an instance method that will be used as an event handler. 
  public void Xhandler() { 
    Console.WriteLine("Event received by object " + id)
  

 
public class EventDemo2 
  public static void Main() {  
    MyEvent evt = new MyEvent()
    X o1 = new X(1)
    X o2 = new X(2)
    X o3 = new X(3)
 
    evt.SomeEvent += new MyEventHandler(o1.Xhandler)
    evt.SomeEvent += new MyEventHandler(o2.Xhandler)
    evt.SomeEvent += new MyEventHandler(o3.Xhandler)
 
    // Fire the event. 
    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.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
4.Create a custom means of managing the event invocation listCreate a custom means of managing the event invocation list
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.