
OOP Development in Visual Basic | 33
OOP
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Overloads keyword—described later in this chapter—must be added to each
declaration. However the
New procedure is a special case; it does not require the
Overloads keyword.)
For classes that require no special initialization of their public or private members,
the constructor can be omitted from the class; Visual Basic will provide a default
constructor when no defined constructor exists in a class. But many classes
require some basic initialization, and the constructor is the place to do it. The
Person class defined earlier includes two constructors.
Public Sub New( )
' ----- Default constructor.
fullName = "<unnamed>"
End Sub
Public Sub New(ByVal newName As String)
' ----- Simple constructor to set an initial field.
fullName = newName
End Sub
The first constructor is the default constructor; since it includes no arguments in
its declaration signature, it is used by default when an instance is created that
lacks any initialization arguments. The second constructor is a custom constructor;
it is called when an instance is created that passes a single string argument.
' ----- Uses the default constructor.
Dim byDefault As Person = New Person
' ----- Uses the custom constructor.
Dim byCustom As Person = New Person("John Q. Public")
The arguments