|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionEvents is one of the fantastic features in C# after delegates. Events is a mechanism of generating notification. In C#, objects of a class can send events to objects of other classes and Objects of classes that receive events can handle events the way they want. In other words, we have objects that are instances of classes. Objects can generate events. Objects can receive and process events. To explain the concept of events we will make use of simple examples. Our First example will consist of two classes: Event Generator and Event Receiver. Here we will show you how the event generator class generates an event and how the event receiver class receives and processes the event. Our second example will consists of three classes: one Event Generator class, and two Event Receiver classes. Here we will show you how the event generator class generates an event and how the event receiving classes receives and processes the event. In our last example we will present you with a real-life scenario of students seeking admission to college and how to simulate this real-life scenario using classes and events. Example1.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
Evt += new LinkToEventHandler(c1_o1.Client1EventHandler);
DoSomething();
}
public static void DoSomething()
{
System.Console.WriteLine("Something Happened! We need to send an event.");
SendEvent();
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
}
OutputSomething Happened! We need to send an event.
EventHandler Called for object Object1 of Client1 class
The above program compiles and runs successfully to produce a desired output. Cool! Now let's have a look at the above program. Looks like something complex stuff! But believe me, it's not. Let me explain the program. The above program consists of two classes:
Cool! So we have two types of classes, an event generating class and an event receiving class. Simple isn't it? Let's develop the program step by step so that it is easy for you to comprehend. P1.csclass Client1
{
}
class EventGeneratorClass
{
}
Now let's proceed. Lets focus our attention to the P1.csclass Client1
{
string name;
}
class EventGeneratorClass
{
}
Now we want to initialize the variable P1.csclass Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
}
class EventGeneratorClass
{
}
OK! Remember, we had mentioned that the P1.csclass Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
}
The name of the event handling function is Cool! We are done with the explanation of the P1.csclass Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static void Main()
{
}
}
The job of event generator class is to send events to its clients i.e. objects of P1.csclass Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static void Main()
{
Client1 c1_o1 = new Client1("Object c1_o1 of Client1 class");
}
}
Here we are creating object Now the time has arrived for understanding the Events. So far we have successfully created two classes:
The P1.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static void Main()
{
Client1 c1_o1 = new Client1("Object c1_o1 of Client1 class");
}
}
Now since P1.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
}
}
Now P1.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
}
The P1.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
EventForClients += new EventHandler(c1_o1.Client1EventHandler);
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
}
OK! Now what remains is the final step! And that is invoking the P1.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
Evt += new LinkToEventHandler(c1_o1.Client1EventHandler);
DoSomething();
}
public static void DoSomething()
{
System.Console.WriteLine("Something Happened! We need to send an event.");
SendEvent();
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
}
OutputSomething Happened! We need to send an event.
EventHandler Called for object Object1 of Client1 class
Bingo! We are done with the explanation. The above program compiles and runs successfully to give the desired output. The execution begins from the entry point function Take the following program where we have multiple instances of the same P2.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
Client1 c1_o2 = new Client1("Object2 of Client1 class");
Client1 c1_o3 = new Client1("Object3 of Client1 class");
Evt += new LinkToEventHandler(c1_o1.Client1EventHandler);
Evt += new LinkToEventHandler(c1_o2.Client1EventHandler);
Evt += new LinkToEventHandler(c1_o3.Client1EventHandler);
DoSomething();
}
public static void DoSomething()
{
System.Console.WriteLine("Something Happened! We need to send an event.");
SendEvent();
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
}
OutputSomething Happened! We need to send an event.
EventHandler Called for object Object1 of Client1 class
EventHandler Called for object Object2 of Client1 class
EventHandler Called for object Object3 of Client1 class
The above program compiles and runs successfully to give the desired output. The event handlers are called for respective objects because they were registered to receive the events generated by the We can also have multiple instances of the multiple Client classes receiving and processing the events generated by the Example2.cspublic delegate void LinkToEventHandler();
class Client1
{
string name;
public Client1(string nameArg)
{
name = nameArg;
}
public void Client1EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class Client2
{
string name;
public Client2(string nameArg)
{
name = nameArg;
}
public void Client2EventHandler()
{
System.Console.WriteLine("EventHandler Called for object {0}", name);
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler Evt;
public static void Main()
{
Client1 c1_o1 = new Client1("Object1 of Client1 class");
Client1 c1_o2 = new Client1("Object2 of Client1 class");
Client1 c1_o3 = new Client1("Object3 of Client1 class");
Client1 c2_o1 = new Client1("Object1 of Client2 class");
Client1 c2_o2 = new Client1("Object2 of Client2 class");
Client1 c2_o3 = new Client1("Object3 of Client2 class");
Evt += new LinkToEventHandler(c1_o1.Client1EventHandler);
Evt += new LinkToEventHandler(c1_o2.Client1EventHandler);
Evt += new LinkToEventHandler(c1_o3.Client1EventHandler);
Evt += new LinkToEventHandler(c2_o1.Client2EventHandler);
Evt += new LinkToEventHandler(c2_o2.Client2EventHandler);
Evt += new LinkToEventHandler(c2_o3.Client2EventHandler);
DoSomething();
}
public static void DoSomething()
{
System.Console.WriteLine("Something Happened! We need to send an event.");
SendEvent();
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
}
OutputSomething Happened! We need to send an event.
EventHandler Called for object Object1 of Client1 class
EventHandler Called for object Object2 of Client1 class
EventHandler Called for object Object3 of Client1 class
EventHandler Called for object Object1 of Client2 class
EventHandler Called for object Object2 of Client2 class
EventHandler Called for object Object3 of Client2 class
The above program compiles and runs successfully to give the desired output. The event handlers are called for respective objects because they were registered to receive the events generated by the Now it's time to present you the last example which shows how to simulate a real life scenario of students seeking admission to college using classes and events. So let's start with a problem statement. Tom, Dick and Harry are three male students. Shaka, Laka and Baby are three female students. They are seeking admission to a college named KC College. The cut off percentage is 90. This means only students who get more than 90% will be notified that they have got admission. The rest of them will be notified that they have not been admitted. On the students part, if they get notified that they haven't got the admission, then thay will apply to some other college. Whereas if they do get notified that they have got the admission, then they will pay the fees. Now let's simulate the above problem using classes and events. Let's have two classes example3.cspublic delegate void LinkToEventHandler(int i);
class MaleStudent
{
string name;
int Marks;
public MaleStudent(string nameArg, int marks)
{
name = nameArg;
Marks = marks;
}
public void MyActionItem(int i)
{
if (i == 1)
System.Console.WriteLine("{0} Got Admission. He must pay fees", name);
else
System.Console.WriteLine(
"{0} did not Get Admission. He must lookout for other college", name);
}
public int GetMarks()
{
return Marks;
}
}
class FemaleStudent
{
string name;
int Marks;
public FemaleStudent(string nameArg, int marks)
{
name = nameArg;
Marks = marks;
}
public void MyActionItem(int i)
{
if (i == 1)
System.Console.WriteLine("{0} got Admission. He must pay fees", name);
else
System.Console.WriteLine(
"{0} did not Get Admission. He must lookout for other college", name);
}
public int GetMarks()
{
return Marks;
}
}
class EventGeneratorClass
{
public static event LinkToEventHandler AdmissionDeniedEvt;
public static event LinkToEventHandler AdmissionGrantedEvt;
public static void Main()
{
MaleStudent Tom = new MaleStudent ("Tom", 95);
MaleStudent Dick = new MaleStudent ("Dick", 89);
MaleStudent Harry = new MaleStudent ("Harry", 91);
FemaleStudent Shaka = new FemaleStudent("Shaka", 92);
FemaleStudent Laka = new FemaleStudent("Laka", 88);
FemaleStudent Baby = new FemaleStudent("Baby", 91);
if (Tom.GetMarks() >= 90)
AdmissionGrantedEvt += new LinkToEventHandler(Tom.MyActionItem);
else
AdmissionDeniedEvt += new LinkToEventHandler(Tom.MyActionItem);
if (Dick.GetMarks() >= 90)
AdmissionGrantedEvt += new LinkToEventHandler(Dick.MyActionItem);
else
AdmissionDeniedEvt += new LinkToEventHandler(Dick.MyActionItem);
if (Harry.GetMarks() >= 90)
AdmissionGrantedEvt += new LinkToEventHandler(Harry.MyActionItem);
else
AdmissionDeniedEvt += new LinkToEventHandler(Harry.MyActionItem);
if (Shaka.GetMarks() >= 90)
AdmissionGrantedEvt += new LinkToEventHandler(Shaka.MyActionItem);
else
AdmissionDeniedEvt += new LinkToEventHandler(Shaka.MyActionItem);
if (Laka.GetMarks() >= 90)
AdmissionGrantedEvt += new LinkToEventHandler(Laka.MyActionItem);
else
AdmissionDeniedEvt += new LinkToEventHandler(Laka.MyActionItem);
if (Baby.GetMarks() >= 90)
AdmissionGrantedEvt += new LinkToEventHandler(Baby.MyActionItem);
else
AdmissionDeniedEvt += new LinkToEventHandler(Baby.MyActionItem);
SendEvent();
}
public static void SendEvent()
{
if (AdmissionGrantedEvt != null)
{
AdmissionGrantedEvt(1);
}
if (AdmissionDeniedEvt != null)
{
AdmissionDeniedEvt(0);
}
}
}
OutputTom Got Admission. He must pay fees
Harry Got Admission. He must pay fees
Shaka got Admission. He must pay fees
Baby got Admission. He must pay fees
Dick did not Get Admission. He must lookout for other college
Laka did not Get Admission. He must lookout for other college
The above program compiles and runs successfully to produce the desired output.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||