The fire alarm example above can be modified to demonstrate the use of the generic EventArg<T> class described previously.
Let's suppose that our fire alarm system has sensors that send us a FireSignal struct with 'room' and 'ferocity' as above. Rather than unpack the FireSignal to create an instance of FireEventArgs, we'll eliminate FireEventArgs and use EventArg<FireSignal> instead. The code below produces identically the same output as the orginal example.
[C#]
// FireSignal: a structure for passing information from the fire sensors.
// An EventArgs class for this information would basicly duplicate
// the code that is already here. Instead, we'll use the FireSignal
// struct in the EventArg<T> generic class that was defined previously.
public struct FireSignal
{
public string room;
public int ferocity;
public FireSignal(string room, int ferocity)
{
this.room = room;
this.ferocity = ferocity;
}
} //end of struct FireSignal
// Class with a function that creates the eventargs and initiates the event
public class FireAlarm
{
public delegate void FireEventHandler(object sender, EventArg<FireSignal> fe);
public event FireEventHandler FireEvent;
public void ActivateFireAlarm(FireSignal fireSignal)
{
FireEvent(this, new EventArg<FireSignal>(fireSignal));
}
} // end of class FireAlarm
// Class that handles the event
class FireHandlerClass
{
// Create a FireAlarm to handle and raise the fire events.
public FireHandlerClass(FireAlarm fireAlarm)
{
fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
}
// This is the function to be executed when a fire event is raised.
void ExtinguishFire(object sender, EventArg<FireSignal> fe)
{
Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());
// Now, act in response to the event.
if (fe.data.ferocity < 2)
Console.WriteLine("This fire in the {0} is no problem. I'm going to pour some water on it.", fe.data.room);
else if (fe.data.ferocity < 5)
Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.", fe.data.room);
else
Console.WriteLine("The fire in the {0} is out of control. I'm calling the fire department!", fe.data.room);
}
} //end of class FireHandlerClass
public class FireEventTest
{
public static void Main()
{
// Create an instance of the class that will be firing an event.
FireAlarm myFireAlarm = new FireAlarm();
// Create an instance of the class that will be handling the event. Note that
// it receives the class that will fire the event as a parameter.
FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);
//Use our class to raise a few events and watch them get handled
//Simulate receiving signals from the fire sensors and activating
//the corresponding alarm.
FireSignal signal;
signal = new FireSignal("Kitchen", 3);
myFireAlarm.ActivateFireAlarm(signal);
signal = new FireSignal("Study", 1);
myFireAlarm.ActivateFireAlarm(signal);
signal = new FireSignal("Porch", 5);
myFireAlarm.ActivateFireAlarm(signal);
return;
} //end of main
} // end of FireEventTest}
// The generic event arguments class (repeated here for reference)
public class EventArg<T> : EventArgs
{
private T eventData;
public EventArg(T data) //constructor
{
eventData = data;
}
public T Data //public property for the argument
{
get { return eventData; }
}
} //End of EventArg<T>