Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Basic.NET 2005
(Page 1 of 5 )
This article teaches you how to declare and handle events using Visual Basic.NET 2005. It
assumes the reader is familiar with object-oriented programming in Visual Basic.NET.
If you are new to OOP in Visual Basic.NET, I strongly suggest you to go through the following
links:
Using Constructors with Object-Oriented Database Development
Properties and Object-Oriented Database Development
Using Methods with Object-Oriented Database Development
Inheritance with VB.NET 2005
The entire source code for this article is available in the form of a downloadable zip file. The
solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft
Windows Server 2003 Enterprise Edition. I didn't really test it in any other environment. I
request that you post in the discussion area if you have any problems in execution.
A sample class without any event declarations: coding the class
Before going directly to learning about events in Visual Basic 2005, let us try to understand the
following class:
Public Class Sample01
Private _x As Integer
Private _y As Integer
The above class has two private fields ("_x" and "_y") which are only accessible within the class,
and not outside the class. Further, it has two public properties and a public method named
"GetCalculatedValue" (public members are accessible even outside the class).
The "GetCalculatedValue" method tries to do division based on the values available in "_x" and
"_y." The denominator ("_y") should never be zero during division. The "if" condition checks
for the same and responds with an appropriate message.
In the next section we will try to test the above class with a Windows form.
To test the class presented in the previous section, add a new form with two buttons and a label.
Modify your code to match the following:
Public Class Form1
Dim obj As New Sample01
End Class
When you hit "Button1," it should give you a "Successful" message and the result in a label. If
you hit "Button2," it gives the error message and the result in the label would be "0."
It is quite impractical (and not at all recommended) to have message boxes in classes. The
business logic classes are meant only for logic and none of them should display any message
boxes. Instead, the messages should be sent to the calling applications (in this case, it is the
form).
In the "GetCalculatedValue" method, we are dealing with two things simultaneously. One of
them is giving a message to the form (the user may use the message for display or simply
suppress messages from displaying), and the other is returning a value back to the form.
Returning a value to the form is simply done using the "Return" statement. How about the
message? How do we send it back professionally? The user may or may not be interested in the
message! If he is interested, he displays the message in another label and if not, he simply
neglects it.
The next section gives you a professional approach for the same issue using events in Visual
Basic 2005.
Understanding Custom Events using Visual
Basic.NET 2005 - Rewriting the sample class
with eventing in Visual Basic 2005: coding
the class
(Page 3 of 5 )
The following is the class which is rewritten to support events available in Visual Basic 2005.
Before trying to understand events, let us go through the following class:
Public Class Sample01
Private _x As Integer
Private _y As Integer
End Class
Let us try to understand the above class. The following is a new statement in the above class:
Public Event CalculationStatus(ByVal Msg As String)
The above declaration says that "CalculationStatus" is an event (part of "Sample01" class) which
may be necessary to handle at the client level (say Windows Form). It carries a message with
the "Msg" parameter.
Every event declared within the class must be fired to notify the client (say Windows Form) to
handle the latest event. If the event is not fired, the client would never know about its message
or progress. To fire an event, "RaiseEvent" will be used. In the above class, we are firing two
times in various situations (as part of an "if" condition).
If you drag a button onto Windows Form, you can observe that it has lots of events to handle.
But mostly you will handle only the "Click" event. Even the "Click" event is not compulsory to
handle. If you don't handle any events, the button simply does nothing. Similarly, with our class,
when instantiated, you have the opportunity to handle the "CalculationStatus" event (which is
still not compulsory). If the Windows Form handles the event, it gives out the message;
otherwise, it does not.
In the next section we try to test the above class with a Windows Form.
To test the class presented in the previous section, add a new form with two buttons and two
labels (one to display the result after division and the other for the message). Modify your code
to match the following:
Public Class Form2
In previous section, if you observe the following statement, it is declared at the class level, which
means that it is outside all the methods:
WithEvents obj As New Sample01
This may not be convenient in every situation as we do create a lot of objects and remove them
every time, especially in loops. The "WithEvents" cannot be available as part of method in a
class. It can only be available with class level variables/objects.
To solve the above situation, the "AddHandler" comes to the rescue. Let us rewrite our testing
class as follows:
Public Class Form2