vkinfotek

Constructors in Visual Basic.Net

Constructors are special methods that provide control over the initialization of Objects.

Any programmer who has worked on OOPs programming will know that there are many situations in which objects have to be initialized, before use. 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.

And to use all member variables and services of the class, we need to instantiate the Class.

A constructor is used in a database application to instantiate the class. One scenario in which we use the constructor is in the business layer. Assume that we are developing a class (tran class) for handling the transactions in a database application. The constructor is used in the Tran Class, to instantiate the class. The below given diagram shows where the constructor fits in a database application. If you would like to know all the steps involved in creating such a class with multiple constructors go through the book titled : Database programming using visual basic, c# (c sharp) sql server

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 windows applications and Page_Load in web applications.

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
  • Instance constructors

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
	
	Shared Sub New()
          x=0  
	End Sub
End Class

We can increment the value of a shared variable in an instance constructor to keep track of the number of instances created in a class. The following code illustrates the use of a shared variable within an instance constructor.

Sub New
x=x+1
MessageBox.Show("Number of instances are:" &i)
End Sub



To test how shared constructor works, create a form and name it as Form1 and place a button Button1.

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 class1 = New class1
Dim c2 As class1 = New class1
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.

Public Class ItemClass
 Private ItemCode As String
 Private ItemName As String
 Private ItemDescription As String
 Private ItemCategory As String
 Private ItemPrice As Single
 Private ItemUnit As String

 Public Sub New(ByVal Category As string)
	ItemCategory = Category
 End Sub

End Class

In the Instance Constructor, the statement, ItemCategory = Category assigns Item Category to class variable ItemCategory.

To test how Instance constructor works, create a form and name it as Form1 and place a button Button1.

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 objItem As New ItemClass("I")
End Sub
End Class

This is how we can instantiate the Item class which in turn calls the instance constructor.

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.





Authorship
By Bharati Karedla