Constructors in Visual Basic 2005
Constructors are special methods that allow control over the
initialization of Objects. To use the member variables and services of the class, we need to instantiate the Class. A constructor is nothing more than a subroutine named New. When the class is instantiated, New (constructor) is fired. We can place the startup code just like we do in Form_Load in win apps and Page_Load in web apps. The life of an object ends after it goes out of scope or is set to Nothing and is released by the .NET Framework. Visual Basic 2005 uses procedure called destructors to control the release of system resources. Constructors and destructors together support the creation and destruction of objects. There are two types of constructors,
shared constructors and
instance constructors. Where do we come across the need for initialization of Objects. The common initialization tasks include opening of files, connecting to a database, and reading the values of registry keys.
Implementation of Shared Constructors
Shared constructors are used to initialize the
shared variables of a type. Shared variables are created using the Shared keyword and store values that can be shared by all the instances of a class. Shared constructors have an
implicit public access. A shared constructor will not run more than once during a single execution of a program.
The following example is an illustration of the shared constructor.
Public Class class1
Shared x As Integer
Dim y As Integer
Shared Sub New()
x=10
y=3
End Sub
End Class
Dim, Public, Private, Friend, Protected and Protected Friend keywords. We can also access shared variables within instance constructor. For example, we can increment the value of a shared variable in an instance constructor to keep track of number of instances created of a class. The following code illustrates the use of a shared variable within an instance constructor. In this example, a form named Form1 and button Button1 is used.
Public Class Form1
Inherits System.windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, Byval e As System.EvenArgs) Handles Button1.Click
Dim c1 As New class1()
Dim c2 As New class2()
End Sub
End Class
Public Class class1
Shared num As Integer
Public Sub New()
num=num+1
MsgBox("number of instances are")
End Sub
Shared Sub New()
num=0
End Sub
End Class
The above code illustrates the use of a shared variable within an instance constructor to keep track of the number of instances of a class.
Instance Constructor in Visual Basic 2005
Instance constructors are used to initialize variables that are declared with Dim, Public, Private, Friend, Protected, and Protected Friend keywords. Write the following code in the class module.
Sub New()
ItemCode = "Ioo1"
ItemName = "rpm Reading Guage"
ItemDescription = "Trading "
ItemCategory = "Instruments"
ItemPrice = 10
ItemUnit = "Kgs"
End Sub
Summary
Note that we have to use Constructors to initialize Objects. This is the procedure provided in Visual Basic.Net to initialize Objects. This helps in having more control over initialization of Objects.
Free Resources