Introduction to VB.NET Object-Oriented Featuresby Wei-Meng Lee
It used to be the case that Visual Basic was regarded as a "toy" language. VB was never treated seriously. This is partly due to the fact that VB does not contain certain advanced features, like pointers access and object-oriented programming. Not anymore! With the launch of the Microsoft .NET framework, Microsoft has catapulted the image of VB programmers to the rank of C and C++ programmers. In this article, I will show you the object-oriented (OO) features of VB.NET and how you can now be on par with your fellow C++ and C# programmers.
The first two terms that you must know are class and object. A class is like a template; it defines the innards of an item. A good analogy is "car." When the word "car" is mentioned, you have the impression of a vehicle with wheels, etc., but no specific make of car is mentioned. An object, in turn, is an instance of a class. A "BMW" is a specific make of a car and hence, it can be likened to an object.
In VB.NET, a class in defined by the following syntax:
Public Class Point End Class
To create an object from a class, you can use the
Dim pt As New Point()
Our First Example
In Listing 1, I have defined a
Public Class Point Private ptX, ptY As Integer Private Shared count As Integer Public Sub New() ' empty constructor count += 1 End Sub ' constructor with two parameters Public Sub New(ByVal x As Integer, ByVal y As Integer) ptX = x ptY = y count += 1 End Sub Property x() As Integer ' sets the X coordinate Get Return ptX End Get Set(ByVal Value As Integer) If Value < 500 And Value > -500 Then ptX = Value End If End Set End Property Property y() As Integer ' sets the Y coordinate Get Return ptY End Get Set(ByVal Value As Integer) ptY = Value End Set End Property ' calculates the length between 2 points Public Function length(ByVal pointOne As Point) As Single Return Math.Sqrt(Math.Pow(ptX - pointOne.x, 2) + _ Math.Pow(ptY - pointOne.y, 2)) End Function ReadOnly Property counter() As Integer ' returns the counter Get Return count End Get End Property End Class
Within the class, I have two private variables:
ptY. These two variables are only visible within the class.
Private ptX, ptY As Integer
I also have a shared variable:
count. This variable would be shared by all instances of the class. That is, all objects of this class would access the same variable.
Private Shared count As Integer
In my class, I have two constructors:
Public Sub New() ' empty constructor count += 1 End Sub ' constructor with two parameters Public Sub New(ByVal x As Integer, ByVal y As Integer) ptX = x ptY = y count += 1 End Sub
The use of a constructor is to initialize the values in an object when it is instantiated (created). A constructor is a subroutine prefixed with the keyword
New. The first constructor simply increments the count variable. The second constructor sets the value of the two private variables with the value of the input parameter.
There are a couple of ways to instantiate an object:
Dim pt1 As New Point(3, 4) Dim pt2 As Point = New Point(5, 6) Dim pt3 As New Point() Dim pt4 As Point
The first creates a
Point object and calls the second constructor shown earlier. It does so by matching the parameter list. The second instantiation statement is similar to the first. The third instantiation statement calls the first constructor shown above, but does not set any values in the object. The fourth simply creates an object of type
Point, but does not actually point to an object. To point to an actual object, it needs to do this:
pt4 = New Point()
Continuing with the class definition, I have also defined two properties,
y, using the
Property x() As Integer ' sets the X coordinate Get Return ptX End Get Set(ByVal Value As Integer) If Value < 500 And Value > -500 Then ptX = Value End If End Set End Property
The private variables are used to store the values set by the properties, as shown below:
'---setting the properties pt3.x = 7 ' stores the value 7 into private variable ptX pt3.y = 9 ' stores the value 9 into private variable ptY Dim x as Integer = pt3.x ' retrieving the value of ptX
Properties also provide a good opportunity to perform checking to ensure that the user does not assign an invalid value to a property.
I also have a method named
length, which returns the length between two points.
Public Function length(ByVal pointOne As Point) As Single Return Math.Sqrt(Math.Pow(ptX - pointOne.x, 2) + _ Math.Pow(ptY - pointOne.y, 2)) End Function
You can use the method through:
Note that this method is public; if it is defined to be a private method like the following, then it cannot be called as above.
Private Function length(ByVal pointOne As Point) As Single
Lastly, I have an additional property called
counter. Notice that this property has the keyword
ReadOnly. This keyword indicates that this property can only be read and cannot be assigned a value such as in the following:
Pt1.counter = 5 ' not allowed! Msgbox (Pt1.counter) ' allowed