A constructor is a special subroutine named New.
Class constructors can take any number of parameters. If you provide no constructors, Visual Basic allows a default empty constructor that takes no parameters. If you provide any constructor, Visual Basic does not provide a default empty constructor. If you want to allow the program to use an empty constructor in that case, you must either provide one or provide a constructor with all optional parameters.
The following code shows a Person class that implements a constructor to make it easier to initialize its properties:
Public Class Person Private m_FirstName As String Public Property FirstName() As String Get Return m_FirstName End Get Set(ByVal value As String) m_FirstName = value End Set End Property Private m_LastName As String Public Property LastName() As String Get Return m_LastName End Get Set(ByVal value As String) m_LastName = value End Set End Property ' Empty constructor. Public Sub New() m_FirstName = "" m_LastName = "" End Sub ' Initialization constructor. Public Sub New(ByVal first_name As String, ByVal last_name As String) m_FirstName = first_name m_LastName = last_name End Sub End Class
Structure constructors are very similar to class constructors with two major exceptions. First, you cannot make an empty structure constructor. Second, Visual Basic always provides a default empty constructor, even if you give the structure other constructors.