You can create classes that raise events. Often, you'll do this when your class is a
control that the user interacts with. For example, your class is a Button control that
the user can click, raising the Click event. There might be other events as well, like
the OnMouseOver event, which is called when the mouse passes over the control.
The second common reason to raise an event is when a class is asynchronous. With an asynchronous
class, the calling method will call a member method that performs a long-running calculation.
Your class does the work in the background, returning control to the calling method. When
the calculation is complete, your class raises an event, and the calling method retrieves
the results by handling the event.
I'll show you how to 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 Visual
Basic 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.
Now i'm going to create the AnswerEventArgs class. Notice that it's inheriting fromt he
standard EventArgs class. It's going to have a single public member called answer. It's
an integer, and i'll populate that value by adding two values provided to my method.
I'm creating the MyCalc class now. First I'm declaring the event using the delegate. I'm
going to name the event OnKnowAnswer, and there's the name of my delegate, KnowAnswer.
Now 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 an instance of AnswerEventArgs. First I'm
going to create an instance of AnswerEventArgs with the name e.
Next I will set the value of e.answer to the sum of the i1 and i2 integer parameters.
Now that we've created that instance and populated the answer, I can actually call the event
by using the RaiseEvent keyword and specifying the OnKnowAnswer event that I declared.
I'm providing two parameters: Me and e. Me represents the instance of the class itself,
and e represents my instance of AnswerEventArgs. Now I just need to create an instance of MyCalc
and call the Add method to raise the event. Raising the event will call my event handler.
Now I am using the AddHandler keyword to declare the event handler that I will declare next.
I'm naming the event handler mc_OnKnowAnswer which is the standard convention.
I'm calling Add with two integers, and then calling the Console.Readkey, which stops the
debugger from closing the window so we can see the results.
Now I'm declaring the subroutine mc_OnKnowAnswer which will act as our event handler. Notice
that I need to add the Object and AnswerEventArgs parameters exactly as they appear in the delegate.
Those must match exactly.
In the event handler I'm going to have a single line that displays the results to the console
by calling Console.WriteLine.
Now to run the application and demonstrate that it runs properly. If this works, we shoudl
see the answer of 8--the sum of 5 plus 3. Great!