AddThis Social Bookmark Button

Print

VB.NET OOP Part 2 - Understanding Constructors

by Budi Kurniawan co-author of VB.NET Core Classes in a Nutshell
11/04/2002

Beginners of OOP with VB.NET often don't pay enough attention to constructors. The reason for this is their classes are usable even without a constructor. What's worse is that many think constructors are just another type of method, which is simply not true. This article focuses on constructors, what they are used for, and some examples on using them. Understanding constructors well helps you write better classes.

For an object to be created from a class, the class needs a constructor. At a glance, a constructor looks just like a method. However, constructors are used for the sole purpose of creating an instance of a class. Here are the characteristics of a constructor:

  • Always defined as a Sub. It therefore does not have a return value.
  • Regardless the name of the class, the constructor in VB.NET is called New.
  • Can have any access modifier (Public, Protected, Friend, Private, or default).
  • In most cases, a constructor has a Public access modifier.
  • A class can have multiple constructors.

Say you write a class called Employee, as given below:

Related Reading

VB.NET Core Classes in a Nutshell
By Budi Kurniawan, Ted Neward


Public Class Employee
  Public Sub Work()
    System.Console.WriteLine("I am working.")
  End Sub
End Class

You can instantiate an object of type Employee by using the New keyword like this:


Dim employee As Employee
employee = New Employee()
employee.Work()

There is nothing in the Employee class definition other than the Work method, yet you still can construct an Employee object. This seems to contradict the previous statement that you need a constructor to instantiate an object of a class. What actually happens is the VB compiler checks for the presence of a constructor in a class it compiles. If none is found, it automatically adds a no-argument constructor. Therefore, the previous Employee class is equivalent to the one below:


Public Class Employee
  Public Sub New()
  End Sub

  Public Sub Work()
    System.Console.WriteLine("I am working.")
  End Sub
End Class

However, if there is a constructor, whether it accepts an argument or not, the compiler will not add a no-argument constructor. For example, if you have a class such as the one below:


Public Class Employee
  Sub New(ByVal name As String)
  End Sub

  Public Sub Work()
    System.Console.WriteLine("I am working.")
  End Sub
End Class

you cannot construct an Employee object using the following code:


Dim employee As Employee
employee = New Employee()

This is because there is now no no-argument constructor in the Employee class. In VB, constructors must be declared as a Sub and in most cases they have a Public access modifier. However, a constructor can also have a Protected, Private, Friend, or the default modifier. For example, you use the Protected or Private modifier in a Singleton class to prevent the user from directly instantiating an object of that class. Singletons will be discussed in the next article, "The Singleton Design Pattern."

The Relationship With the Base Class

If a child class extends a base class, the compiler will make a call from the child class' constructor to the base class' no-argument constructor. The call will be invoked from the first statement of the child class' constructor. For example, consider the Employee class and the Manager class that extends Employee, as in the following code:


Public Class Employee
  Public Sub New()
    System.Console.WriteLine("Employee's constructor.")
  End Sub

  Public Sub Work()
    System.Console.WriteLine("I am working.")
  End Sub
End Class

Public Class Manager : Inherits Employee
  Public Sub New()
    System.Console.WriteLine("Manager's constructor.")
  End Sub
End Class

When the Manager class' constructor is invoked, like in the following code, the Employee class' no-argument constructor is also called.


Dim manager As Manager
manager = New Manager()

The result on the console is as follows:


Employee's constructor.
Manager's constructor.

Note that the code in the base class' constructor is run before the code in the child class' constructor. If the base class does not have a no-argument constructor, the class won't compile. For example, the following code will produce a compile error because the Employee class (base class) does not have a no-argument constructor.


Public Class Employee
  Public Sub New(ByVal name As String)
    System.Console.WriteLine("Employee's constructor.")
  End Sub

  Public Sub Work()
    System.Console.WriteLine("I am working.")
  End Sub
End Class

Public Class Manager : Inherits Employee
  Public Sub New()
    System.Console.WriteLine("Manager's constructor.")
  End Sub
End Class

This compile error occurs because the compiler adds a call to the base class' no-argument constructor's on the first line of the child class' constructor. One solution to this problem is to add a no-argument constructor to the base class. However, if you don't want to do this, you can use the MyBase keyword to reference any constructor in the base class. Note that since MyBase is present in the child class' constructor, the compiler does not add another call to the base class' no-argument constructor. The MyBase statement must be the first line in the constructor:


Public Class Employee
  Public Sub New(ByVal name As String)
    System.Console.WriteLine( _
      "Employee's constructor with one argument.")
End Sub

  Public Sub Work()
    System.Console.WriteLine("I am working.")
  End Sub
End Class

Public Class Manager : Inherits Employee
  Public Sub New(ByVal name As String)
    MyBase.New(name)
    System.Console.WriteLine("Manager's constructor.")
  End Sub
End Class

Constructor calls are only valid as the first statement in a constructor. Even if a class does not explicitly extend another class, you can still call the base class' constructor because every class implicitly extends the System.Object class if no class is explicitly defined as the base class. Therefore, the constructor in the Manager class below is valid:


Public Class Manager
  Public Sub New(ByVal name As String)
    MyBase.New()
    System.Console.WriteLine("Manager's constructor.")
  End Sub
End Class

Thus, MyBase.New() in the Manager class' constructor calls the System.Object class' no-argument constructor.

Pages: 1, 2

Next Pagearrow