You can create classes that methods that create an instance of your class can respond to.
The most common reason to do this is that your class is a control that the user interacts
with. For example, the Button control calls the Click event when the user clicks it. There
are other events, such as MouseOver, that is called when the user moves the mouse over
the control.
The second common reason that you want to raise an event is because a class is asynchronous.
In an asynchronous class, the calling method will call one of your classes member methods
that performs a long-running calculation. Your class then does the work in the background,
returning control to the original method. Your class, when the calculation is complete,
can then raise the event and allowing the method that created an instance of your class
to respond to it.
I'll show you how to quickly create a simple class that raises an event.
I'm going to create a class called MyCalc that performs a calculation when the calling
method calls a member method. Instead of returning it the conventional way, it's going to return
it by raising an event.
The first thing we need to do is to create a delegate for the event. You do that in C#
using the delegate keyword. I'm going to call this KnowAnswer.
I'm going to provide two parameters: the standard Object, which represents the method that sent
it, and an instance of AnswerEventArgs.
I haven't yet created AnswerEventArgs, but I'm going to do that now by deriving it from
the EventArgs standard class built into the .NET Framework. IT automatically inherits
all of the EventArgs members, and I'm going to add one more member called answer which
is just an integer, in which the resulting class can place an integer.
Now I'm going to create a class called MyCalc. I'll create an event using the event keyword.
That's the name of the delegate, and now I'll actually create the event by naming it OnKnowAnswer.
I'm going to create a method called Add that adds two integers.
I'm going to use those two integers to populate the answer member of AnswerEventArgs. First
I'm going to create an instance of AnswerEventArgs.
Now that we've created that instance and populated the answer, I can actually call the event.
Now, by providing the first parameter as this, I'm providing an instance of the class itself.
The event handler could use that in some way; I won't use it in this example, but sometimes
it is useful. The second parameter, of course, is the instance of AnswerEventArgs which actually
contains the result of the addition.
Now I just need to create an instance of my MyCalc class and call the Add method to raise
the event, which will be handled by my event handler.
First I'll create an instance of the class. Next, I'll let VIsual Studio automatically
generate the event handler--you'll see it automatically generates it there.
Next I'm going to use the += operator to add the event handler. Notice that I haven't created
the event handler yet; Visual Studio can do this automatically. I'm going ot press the
Tab key twice, and it automatically generates a method called mc_OnKnowAnswer.
Notice that it automatically generates code that throws an exception. I'm just going to
remove that and add in a line that simply displays the answer to the console.
Notice that I can access the answer member of my custom AnswerEventArgs class.
Now I just need to call the Add method and have it add any two integers. I'll add a call
to Console.ReadKey to pause the console.
Run the application, and as you can see, it successfully displays the answer.