Csharp/C Sharp/Language Basics/Event Handler
Demonstrate passing an object to an event handler and performing the proper cast in the method
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// ObjEvent.cs -- Demonstrate passing an object to an event handler and
// performing the proper cast in the method.
//
// Compile this program with the following command line:
// C:>csc ObjEvent.cs
using System;
namespace nsEvents
{
public class ObjEvent1
{
public delegate void EventHandler (object obj);
public event EventHandler EvInvoke;
public void FireEvent (object obj)
{
if (obj != null)
EvInvoke (obj);
}
static public void Main ()
{
ObjEvent1 main = new ObjEvent1 ();
main.EvInvoke = new ObjEvent1.EventHandler (ObjEvent);
main.FireEvent (42);
main.FireEvent (42.0);
}
static void ObjEvent (object obj)
{
if (obj is double)
{
Console.WriteLine ("Received a double object: " + (double) obj);
}
else if (obj is int)
{
Console.WriteLine ("Received an int object: " + (int) obj);
}
}
}
}
illustrates the use of an event
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example12_4.cs illustrates the use of an event
*/
using System;
// declare the MeltdownEventArgs class (implements EventArgs)
class MeltdownEventArgs : EventArgs
{
// declare a private field named message
private string message;
// define a constructor
public MeltdownEventArgs(string message)
{
this.message = message;
}
// define a property to get the message
public string Message
{
get
{
return message;
}
}
}
// declare the Reactor class
class Reactor
{
// declare a private field named temperature
private int temperature;
// declare a delegate class named MeltdownHandler
public delegate void MeltdownHandler(
object reactor,
MeltdownEventArgs myMEA
);
// declare an event named OnMeltdown
public event MeltdownHandler OnMeltdown;
// define a property to set the temperature
public int Temperature
{
set
{
temperature = value;
// if the temperature is too high, the reactor melts down
if (temperature > 1000)
{
MeltdownEventArgs myMEA =
new MeltdownEventArgs("Reactor meltdown in progress!");
OnMeltdown(this, myMEA);
}
}
}
}
// declare the ReactorMonitor class
class ReactorMonitor
{
// define a constructor
public ReactorMonitor(Reactor myReactor)
{
myReactor.OnMeltdown +=
new Reactor.MeltdownHandler(DisplayMessage);
}
// define the DisplayMessage() method
public void DisplayMessage(
object myReactor, MeltdownEventArgs myMEA
)
{
Console.WriteLine(myMEA.Message);
}
}
public class Example12_4
{
public static void Main()
{
// create a Reactor object
Reactor myReactor = new Reactor();
// create a ReactorMonitor object
ReactorMonitor myReactorMonitor = new ReactorMonitor(myReactor);
// set myReactor.Temperature to 100 degrees Centigrade
Console.WriteLine("Setting reactor temperature to 100 degrees Centigrade");
myReactor.Temperature = 100;
// set myReactor.Temperature to 500 degrees Centigrade
Console.WriteLine("Setting reactor temperature to 500 degrees Centigrade");
myReactor.Temperature = 500;
// set myReactor.Temperature to 2000 degrees Centigrade
// (this causes the reactor to meltdown)
Console.WriteLine("Setting reactor temperature to 2000 degrees Centigrade");
myReactor.Temperature = 2000;
}
}
Shows how multiple objects may subscribe to the same event
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Subscrib.cs -- Shows how multiple objects may subscribe to the same
// event.
//
// Compile this program with the following command line:
// C:>csc Subscrib.cs
using System;
namespace nsEvents
{
public class Subscrib
{
// Declare an instance of the clsDelegate class. The event variable
// is not static.
static public clsDelegate dlg = new clsDelegate ();
static public void Main ()
{
// Add clsMain to the event list
dlg.DoEvent += new clsDelegate.StringHandler (ShowEvent);
// Create subscribers for the event
clsSubscriber sub = new clsSubscriber ();
clsNextSubscriber sub2 = new clsNextSubscriber ();
// Fire the event.
dlg.FireEvent ("Fired from Main()");
}
static public void ShowEvent (string str)
{
Console.WriteLine ("Main handled event: " + str);
}
}
public class clsDelegate
{
// Declare a delegate for the event
public delegate void StringHandler (string str);
// A variable to hold the delegate
public event StringHandler DoEvent;
// This method will trigger the event.
public void FireEvent (string str)
{
if (DoEvent != null)
DoEvent (str);
}
}
public class clsSubscriber
{
public clsSubscriber ()
{
Subscrib.dlg.DoEvent +=
new clsDelegate.StringHandler (SubscribeEvent);
}
public void SubscribeEvent (string str)
{
Console.WriteLine ("Subscriber handled event: " + str);
}
}
public class clsNextSubscriber
{
public clsNextSubscriber ()
{
Subscrib.dlg.DoEvent +=
new clsDelegate.StringHandler (SubscribeEvent);
}
public void SubscribeEvent (string str)
{
Console.WriteLine ("Next Subscriber handled event: " + str);
}
}
}