/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use the bult-in EventHandler delegate.
using System;
// Declare an event class.
class MyEvent {
public event EventHandler SomeEvent; // uses EventHandler delegate
// This is called to fire SomeEvent.
public void OnSomeEvent() {
if(SomeEvent != null)
SomeEvent(this, EventArgs.Empty);
}
}
public class EventDemo6 {
static void handler(object source, EventArgs arg) {
Console.WriteLine("Event occurred");
Console.WriteLine("Source is " + source);
}
public static void Main() {
MyEvent evt = new MyEvent();
// Add handler() to the event list.
evt.SomeEvent += new EventHandler(handler);
// Fire the event.
evt.OnSomeEvent();
}
}
|