Events
A class defines an event member to notify other objects
For example, when a Button object is clicked, a Form object receives a notification and performs some action.
The CLR’s event model is based on delegates
Event member
An event member declaration provides the class with three capabilities
Objects can register for the event
Objects can unregister the event
The type that defines the event maintain the set of registered objects and notifies these objects when the event is raised.
public event AlarmMessageEventhandler AlarmMessage when viewed in ILDASM, notice that compiler translates the above line to following three constructs private AlarmMessageEventhandler, add_AlarmMessage, remove_AlarmMessage.
BurglarSystemManager.cs
namespace Event_Demo
{
/// <summary>
/// I have declared a class named BurglarSystemManager
/// </summary>
class BurglarSystemManager
{
/// <summary>
/// We have defined a type that holds additional information to be sent via the event notification
/// Notice that the BurglarAlarmMsgEventArgs inherits from EventArgs. The EventArgs is defined iin the
/// .NET Framework class library
/// </summary>
public class BurglarAlarmMsgEventArgs:EventArgs
{
public readonly string AlarmMessage, AlarmMessageDescription;
public BurglarAlarmMsgEventArgs(string Message, string MsgDescription)
{
this.AlarmMessage = Message;
this.AlarmMessageDescription = MsgDescription;
}
}
// The next step is to define the prototype of the call back method or delegate that the receivers
//has to impliment
public delegate void AlarmMessageEventHandler(object sender, BurglarAlarmMsgEventArgs eventArgs);
// Declare the event member
public event AlarmMessageEventHandler BurglarAlarmMsg;
// Define a protected, virtual method for notifying the registered objects about the events.
protected virtual void OnAlarmMessage(BurglarAlarmMsgEventArgs msgEventArgs)
{
// Check if any object has registered for event message
if(BurglarAlarmMsg != null)
{
//Notify all the objects in the delegate linked list
BurglarAlarmMsg(this, msgEventArgs);
}
}
// Write a method that will handle the message raised by the burglar alarm system
public void HandleAlarmMessagefromSystem(string msg, string desc)
{
BurglarAlarmMsgEventArgs obj = new BurglarAlarmMsgEventArgs(msg, desc);
//Call the virtual method notifying that the alarm from system arrived. Raise the event to notify
// all the intrested listners
OnAlarmMessage(obj);
}
}
}
Form1.cs
namespace Event_Demo
{
/// <summary>
/// Now lets design a type that listens for the alarm event raised
/// </summary>
public partial class Form1 : Form
{
BurglarSystemManager bsm = new BurglarSystemManager();
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
// Constrcut a instance of alarmmsgeventhandler
bsm.BurglarAlarmMsg += new BurglarSystemManager.AlarmMessageEventHandler(AlarmMessage);
}
//Lets write the call back methid that the BurglarAlarmManager will call to
//Notify the alarm system that a new alarm message has been raised
private void AlarmMessage(object sender, BurglarSystemManager.BurglarAlarmMsgEventArgs args)
{
MessageBox.Show("Alarm message Arrived with " + args.AlarmMessage + " Desc " + args.AlarmMessageDescription);
}
/// <summary>
/// Unregister the Burglar alarm message event
/// </summary>
public void UnregisterEvent()
{
bsm.BurglarAlarmMsg -= new BurglarSystemManager.AlarmMessageEventHandler(AlarmMessage);
}
private void Button3_Click(object sender, EventArgs e)
{
bsm.HandleAlarmMessagefromSystem("Alarm raised at", DateTime.Now.ToString());
}
}
}
The demo video is available at
https://youtu.be/UNanaAZO3Ps
A class defines an event member to notify other objects
For example, when a Button object is clicked, a Form object receives a notification and performs some action.
The CLR’s event model is based on delegates
Event member
An event member declaration provides the class with three capabilities
Objects can register for the event
Objects can unregister the event
The type that defines the event maintain the set of registered objects and notifies these objects when the event is raised.
public event AlarmMessageEventhandler AlarmMessage when viewed in ILDASM, notice that compiler translates the above line to following three constructs private AlarmMessageEventhandler, add_AlarmMessage, remove_AlarmMessage.
BurglarSystemManager.cs
namespace Event_Demo
{
/// <summary>
/// I have declared a class named BurglarSystemManager
/// </summary>
class BurglarSystemManager
{
/// <summary>
/// We have defined a type that holds additional information to be sent via the event notification
/// Notice that the BurglarAlarmMsgEventArgs inherits from EventArgs. The EventArgs is defined iin the
/// .NET Framework class library
/// </summary>
public class BurglarAlarmMsgEventArgs:EventArgs
{
public readonly string AlarmMessage, AlarmMessageDescription;
public BurglarAlarmMsgEventArgs(string Message, string MsgDescription)
{
this.AlarmMessage = Message;
this.AlarmMessageDescription = MsgDescription;
}
}
// The next step is to define the prototype of the call back method or delegate that the receivers
//has to impliment
public delegate void AlarmMessageEventHandler(object sender, BurglarAlarmMsgEventArgs eventArgs);
// Declare the event member
public event AlarmMessageEventHandler BurglarAlarmMsg;
// Define a protected, virtual method for notifying the registered objects about the events.
protected virtual void OnAlarmMessage(BurglarAlarmMsgEventArgs msgEventArgs)
{
// Check if any object has registered for event message
if(BurglarAlarmMsg != null)
{
//Notify all the objects in the delegate linked list
BurglarAlarmMsg(this, msgEventArgs);
}
}
// Write a method that will handle the message raised by the burglar alarm system
public void HandleAlarmMessagefromSystem(string msg, string desc)
{
BurglarAlarmMsgEventArgs obj = new BurglarAlarmMsgEventArgs(msg, desc);
//Call the virtual method notifying that the alarm from system arrived. Raise the event to notify
// all the intrested listners
OnAlarmMessage(obj);
}
}
}
Form1.cs
namespace Event_Demo
{
/// <summary>
/// Now lets design a type that listens for the alarm event raised
/// </summary>
public partial class Form1 : Form
{
BurglarSystemManager bsm = new BurglarSystemManager();
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
// Constrcut a instance of alarmmsgeventhandler
bsm.BurglarAlarmMsg += new BurglarSystemManager.AlarmMessageEventHandler(AlarmMessage);
}
//Lets write the call back methid that the BurglarAlarmManager will call to
//Notify the alarm system that a new alarm message has been raised
private void AlarmMessage(object sender, BurglarSystemManager.BurglarAlarmMsgEventArgs args)
{
MessageBox.Show("Alarm message Arrived with " + args.AlarmMessage + " Desc " + args.AlarmMessageDescription);
}
/// <summary>
/// Unregister the Burglar alarm message event
/// </summary>
public void UnregisterEvent()
{
bsm.BurglarAlarmMsg -= new BurglarSystemManager.AlarmMessageEventHandler(AlarmMessage);
}
private void Button3_Click(object sender, EventArgs e)
{
bsm.HandleAlarmMessagefromSystem("Alarm raised at", DateTime.Now.ToString());
}
}
}
The demo video is available at
https://youtu.be/UNanaAZO3Ps
Comments
Post a Comment