// Compare the classic .NET self-contained event parameters, through four types of fire event parameter classes, fire event alarm (FireAlarm), FireHandlerClass, and finally demonstrate events through class FireEventTest Process
Using system;
// FireEventArgs: a Custom Event inherited from Eventargs.//
Public class fireeventargs: Eventargs {public fireEventArgs (string room, int fire;) {this.room = room; this.ferocity = ferocity;}
// the fire es, AND 2) How "Ferocious" IS.
PUBLIC STRING; PUBLIC INT Ferocity
} // end of class fireeventargs
// Class with a function That Creates The Eventargs and Initiates The Eventpublic Class FireAlarm {
// Events Are Handled with Delegates, SO Weme Must Establish A FireEventhandler // AS A DELEGATE:
Public Delegate Void FireEventHandler (Object Sender, FireEventArgs Fe);
// Now, create a public event "fireevent" whose type is our fireeventhandler delegate.
Public Event FireEventHandler FireEvent;
// this will be the starting point of our evenet - It will will create fireeventargs, // and then raise the event, passing fireeventargs.
Public void ActivateFireAlarm (String Room, Int Ferocity) {
FireEventArgs Fireargs = New FireEventArgs (Room, Ferocity);
.
FireEvent (this, fireargs);}} // end of class fireAlarm
// Class Which Handles The Event
Class firehandlerclass {
// Create A FireAlarm to Handle and Raise The Fire Events.
Public firehandlerclass (FireAlarm FireAlarm) {
// Add a delegate containing the ExtinguishFire function to the class' // event so that when FireAlarm is raised, it will subsequently execute // ExtinguishFire.fireAlarm.FireEvent = new FireAlarm.FireEventHandler (ExtinguishFire);}
// this is the function to bee executed when a fire event is raised. Void EXTINGUISHFIRE (Object Sender, FireEventArgs Fe) {
Console.writeline ("/ NTHE EXTINGIRE FUNCTION WAS CALLED BY {0}.", Sender.tostring ());
// now, act in response to the evenet.
IF (Fe.ferocity <2) Console.writeline ("this fire in the {0} is no problem. I'm going to pour some water on it."; Else IF (Fe.ferocity <5 ) Console.Writeline ("I'm Using FireExtinguisher to Put the Fire In The {0};" The Fire In The {0} IS OUT OF Control. I'm Calling the fire department! ", fe.room);}} // end of class firehandlerclass
Public class fireEventtest {public static void main () {
// create an instance of the class thing will be firing an event.
FireAlarm MyfireAlarm = new fireAlarm (); // Create An Instance of the Class That Will Be Handling The Event. Note That Weft As Class That Will Fire The Event As a parameter.
FireHandlerClass myFireHandler = new (myFireAlarm) FireHandlerClass; // use our class to raise a few events and watch them get handled myFireAlarm.ActivateFireAlarm ( "Kitchen", 3); myFireAlarm.ActivateFireAlarm ( "Study", 1); myFireAlarm.ActivateFireAlarm ( "Porch", 5); return;
} // end of main