|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionAn event is a mechanism via which a class can notify its clients when something happens. For example when you click a button, a button-click-event notification is sent to the window hosting the button. Events are declared using delegates. So if you don't know what a delegate is, you may go and read my article on delegates first. The first time you go through this article you might find it confusing, don't worry about that. Just try out the sample program and go through the sample source, line by line. Maybe, you can read the article once more after that. Once you get the hang of it, things will seem simple. By the way this article is intended for beginners and is not meant for advanced level programmers. The ProgramI include the full program below. It's not commented but later down this article, I have taken the program part by part and explained each part. I have also included the output you'll get on running the program. using System;
public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);
public class DivBySevenEventArgs : EventArgs
{
public readonly int TheNumber;
public DivBySevenEventArgs(int num)
{
TheNumber = num;
}
}
public class DivBySevenListener
{
public void ShowOnScreen(object o, DivBySevenEventArgs e)
{
Console.WriteLine(
"divisible by seven event raised!!! the guilty party is {0}",
e.TheNumber);
}
}
public class BusterBoy
{
public static event DivBySevenHandler EventSeven;
public static void Main()
{
DivBySevenListener dbsl = new DivBySevenListener();
EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
GenNumbers();
}
public static void OnEventSeven(DivBySevenEventArgs e)
{
if(EventSeven!=null)
EventSeven(new object(),e);
}
public static void GenNumbers()
{
for(int i=0;i<99;i++)
{
if(i%7==0)
{
DivBySevenEventArgs e1 = new DivBySevenEventArgs(i);
OnEventSeven(e1);
}
}
}
}
//Output
F:\c#\events>1
divisible by seven event raised!!! the guilty party is 0
divisible by seven event raised!!! the guilty party is 7
divisible by seven event raised!!! the guilty party is 14
divisible by seven event raised!!! the guilty party is 21
divisible by seven event raised!!! the guilty party is 28
divisible by seven event raised!!! the guilty party is 35
divisible by seven event raised!!! the guilty party is 42
divisible by seven event raised!!! the guilty party is 49
divisible by seven event raised!!! the guilty party is 56
divisible by seven event raised!!! the guilty party is 63
divisible by seven event raised!!! the guilty party is 70
divisible by seven event raised!!! the guilty party is 77
divisible by seven event raised!!! the guilty party is 84
divisible by seven event raised!!! the guilty party is 91
divisible by seven event raised!!! the guilty party is 98
F:\c#\events>
ExplanationOkay. I presume you have taken a look at the above program and I bet you have all guessed it's purpose. We generate some numbers and every time we generate a number that is divisible by 7 we raise an event. The event handler will print out a message saying that the event was raised and it also prints out the number responsible for raising the event. I guess some of you are frowning and saying that's a stupid reason to raise an event. I know, I know alright. The program is not intended to be used for any useful purpose. It's only an attempt to make events comprehensible. I hope it served it's role out. :-) Okay, so the first thing we did was to declare a delegate. public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);
The delegate defines the parameters sent to the event handlers. Thus any
class that wants to handle this event must have a handler method which has the
same return type and argument list as this delegate. Here as you can see, the
first parameter is an object. In real-world cases event handlers are normally
passed a reference to the sending object. I am not doing that in this program. I
am simply passing a Now, we define the public class DivBySevenEventArgs : EventArgs
{
public readonly int TheNumber;
public DivBySevenEventArgs(int num)
{
TheNumber = num;
}
}
As you can see, it has a public read-only member which is used to store our generated number that is divisible by 7. Normally you should use properties but for the sake of simplicity I am using a public member variable. Now we define our listener class which is the class that needs to be notified of the event. public class DivBySevenListener
{
public void ShowOnScreen(object o, DivBySevenEventArgs e)
{
Console.WriteLine(
"divisible by seven event raised!!! the guilty party is {0}",
e.TheNumber);
}
}
As you can see, it has a function Now, let's examine our public static event DivBySevenHandler EventSeven;
An event is declared like a delegate type variable, except that the keyword Now let's take a look at the function that invokes the event and thus notifies all clients. public static void OnEventSeven(DivBySevenEventArgs e)
{
if(EventSeven!=null)
EventSeven(new object(),e);
}
Let's look at the function public static void GenNumbers()
{
for (int i=0;i<99;i++)
{
if(i%7==0)
{
DivBySevenEventArgs e1 = new DivBySevenEventArgs(i);
OnEventSeven(e1);
}
}
}
We use the Now lets go through public static void Main()
{
DivBySevenListener dbsl = new DivBySevenListener();
EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
GenNumbers();
}
We first create a ConclusionWell you have just seen how you can create events and event handlers. You must remember that events can be invoked only from the class that declared them. This causes problems with regard to inheritance. Thus if you have a class with an event you'd better make your OnWhateverEvent() function protected so that the derived classed can call it. Better still, make it virtual too, so they can override it in their class.
|
||||||||||||||||||||||