Class_Initialize Changes for Visual Basic 6.0 Users

A constructor is a method that is run when an object is created. Constructors are new to Visual Basic. They combine the behavior of the New statement and the Class_Initialize method of Visual Basic 6.0, yet offer more flexibility and control in creating class instances. For more information on object creation, see Creating and Using Objects.


Visual Basic 6.0

Visual Basic 6.0 provides support for the constructor concept through the Class_Initialize method. This method is private and allows no parameters. This method is called automatically when a new instance of the class is created. A call to the New keyword calls the Class_Initialize method, if it exist.

Suppose you want to specify the yearly growth of a tree with a default of five years. The class code would look something like this:

' Visual Basic 6.0
Private mvarYearlyGrowth As Integer

Public Property Get YearlyGrowth() As Integer
    YearlyGrowth = mvarYearlyGrowth
End Property

Public Property Let YearlyGrowth(ByVal newValue As Integer)
    mvarYearlyGrowth = newValue
End Property

Private Sub Class_Initialize()
    mvarHeight = 5
End Sub

The code to create a tree and set the property would look like this:

Dim growingTree As New Tree
growingTree.YearlyGrowth = 10

Visual Basic 2008

Visual Basic 2008 supports constructors through the New keyword. In this case, the additional code to set the YearlyGrowth property is not required. A call to the New keyword calls one of the overloaded New methods in the class. If the class contains no New method, then one without parameters is created by the compiler.

Option Strict On
PublicClass Tree
  Private yearlyGrowthValue AsInteger = 5

  PublicProperty YearlyGrowth() AsIntegerGetReturn yearlyGrowthValue
      EndGetSet(ByVal Value AsInteger)
          yearlyGrowthValue = Value
      EndSetEndPropertyPublicSubNew(ByVal newYearlyGrowth AsInteger)
      Me.YearlyGrowth = newYearlyGrowth

The code to create a tree and set the property would look like this:

Dim growingTree AsNew Tree(10)

In this example, it is not possible to create a Tree without specifying the yearly growth. To allow that, you could add another New method to the class that has no parameters. This is called overloading the constructor.

New Keyword

Visual Basic 6.0

In Visual Basic 6.0, this line of code is generally not recommended:

Dim growingTree As New Tree

This is because whenever the growingTree variable is accessed, the compiler checks if the value is Nothing. If so, a new instance is created and assigned to growingTree. This is not only inefficient, but leads to programming errors.

Visual Basic 2008

In Visual Basic 2008, the instance is not checked and no new instances are created. The only way to create a new instance is to execute a line of code with the New or As New keywords. In effect, the line of code above is now the recommended way to handle creating a new instance.

Upgrade Suggestions

The Upgrade Wizard upgrades the Class_Initialize method to this:

'UPGRADE_NOTE: Class_Initialize was upgraded to Class_Initialize_Renamed. Click for more: ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1061"'Private mvarHeight AsShortPrivateSub Class_Initialize_Renamed()
    mvarHeight = 0

You may be able to eliminate the call to Class_Initialize_Renamed and move the code directly into the constructor. This could make your code more readable as it eliminates an unneeded call to a procedure. If each call to New is commonly followed by setting a particular group of properties, consider adding a constructor that sets those properties. The Visual Basic 6.0 code could be replaced by this code in Visual Basic 2008:

PublicSubNew(ByVal newYearlyGrowth AsInteger)
    Me.YearlyGrowth = newYearlyGrowth

See Also


How to: Use the New Keyword

Other Resources

Creating and Using Objects