Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

Coding New Instances of Objects

Adding Data to Instantiation

By Dan Mabbutt, About.com

Jun 26 2007

It may not look like it, but New in the examples above is actually a method in the object. In other words, there is a New subroutine that is being called. This is where any initialization for the object takes place. The New subroutine is called the constructor. In Visual Basic and C#, the MSIL name for the constructor is CTOR and you view that with ILDASM too. In the extremely simple (actually, empty) class I used earlier, the CTOR MSIL code looks like this:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The .NET way to create an object is very different from VB 6 where the compiler creates the instance and then raises the Initialize event. You can do most initialization in the event subroutine, but you don't have the control that you have in VB.NET to initialize the object.

Both with many Framework objects and in objects that you create with your own code, you can initialize the object in different ways by coding different constructors using overloading. The "catch" is the same one that governs overloading with methods: Each one has to have a unique signature. The signature of a method consists of its name, parameters, and return type. So these three constructors all have different signatures:

Sub New() As String
Sub New(ByVal Parm As Integer)
Sub New(ByVal Parm As String)

It's possible that you might want to use the same signature. For example, you might want to initialize a customer object with names from A to M in one case and N to Z in another. If you have a situation like this, you'll have to do it with a second initialization method. This pattern is often called two-phase construction.

Normally, it's not much trouble to create different signatures because the constructors will actually be doing different types of initialization. For example, a constructor with no parameters might be initialized using a default value while a specific value would be used if present.

Public Class NewClass
   Dim m_InstanceVariable As String
   Sub New()
      m_InstanceVariable = "Default Value"
   End Sub
   Sub New(ByVal Parm As String)
      m_InstanceVariable = Parm
   End Sub
   ReadOnly Property InstanceValue()
      Get
         Return m_InstanceVariable
      End Get
   End Property
End Class

The code to call the different constructors looks like this.

Dim Instance1 As New NewClass
Debug.WriteLine(Instance1.InstanceValue)
Dim Instance2 As New NewClass("Passed During Instantiation")
Debug.WriteLine(Instance2.InstanceValue)

The illustration below demonstrates what happens when the code is executed:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The syntax is a little inconsistent. The parameter is passed with the class name rather than the New method name. If you're using overloaded New constructors, however, the parameter switches back to the New method. It seems that Microsoft just couldn't make up their mind! New constructors can't be flagged as Overridable like normal methods either.

A constructor with no parameters is called a default constructor. Normally, VB.NET will add a default constructor to your program if you don't code one yourself. (Again, check the MSIL if you want to verify this.) But there's one tricky little condition is that could trip you up. Once you add a constructor to a class of any type, VB.NET doesn't add the default constructor anymore. So, starting with the code we just looked at, let's eliminate the default constructor and keep the constructor with a parameter. This breaks the code as shown ...

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

This can, however, be used to your advantage. If you write a class that requires initialization, you can force programmers to write code to pass it when they use your class. If they don't they get the error shown.

Another initialization variation is called a shared constructor. If you code a Shared New method, called a shared constructor, in a class ...

Public Class Form1
   Private Sub Form1_Load( ...
      Debug.WriteLine(SharedConstructorClass.sharedField)
   End Sub
End Class

Class SharedConstructorClass
   Public Shared sharedField As String
   ' shared constructor
   Shared Sub New()
      sharedField = "Initialized by CLR!!"
   End Sub
End Class

... then when the class is used, the constructor is called automatically by the common language runtime (CLR). The code above displays "Initialized by CLR!!" in the Immediate window.

Now that you know all about New, you should be able to write much more constructive code!

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Coding New Instances of Objects

©2009 About.com, a part of The New York Times Company.

All rights reserved.