Let’s switch to a few interesting topics in programming with objects. Objects are instances of classes, and classes are instantiated with the New keyword. The New keyword can be used with a number of arguments, which are the initial values of some of the object’s basic properties. To construct a rectangle, for example, you can use these two statements:
Dim shape1 As Rectangle = New Rectangle()
shape1.Width = 100
shape1.Height = 30
Code language: VB.NET (vbnet)
or the following one:
Dim shape1 As Rectangle = New Rectangle(100, 30)
Code language: VB.NET (vbnet)
The objects in the Minimal class can’t be initialized to specific values of their properties and they expose the simple form of the New constructor — the so-called parameterless constructor. Every class has a parameterless constructor, even if you don’t specify it. You can implement parameterized constructors, which allow you to pass arguments to an object as you declare it. These arguments are usually the values of the object’s basic properties. Parameterized constructors don’t pass arguments for all the properties of the object; they expect only enough parameter values to make the object usable.
Parameterized constructors are implemented via Public subroutines that have the name New. You can have as many overloaded forms of the New() subroutine as needed. Most of the built-in classes provide a parameterless constructor, but the purists of OOP will argue against parameterless constructors. Their argument is that you shouldn’t allow users of your class to create invalid instances of it. A class for describing customers, for example, should expose at least a Name property. A class for describing books should expose a Title and an ISBN property. If the corresponding constructor requires that these properties be specified before creating an instance of the class, you’ll never create objects with invalid data.
Let’s add a parameterized constructor to our Contact class. Each contact should have at least a name; here’s a parameterized constructor for the Contact class:
Public Sub New(ByVal CompanyName As String)
MyBase.New()
Me.CompanyName = CompanyName
End Sub
Code language: VB.NET (vbnet)
The code is trivial, with the exception of the statement that calls the MyBase.New() subroutine. MyBase is an object that lets you access the members of the base class (a topic that’s discussed in detail later in this chapter). The reason you must call the New method of the base class is that the base class might have its own constructor, which can’t be called directly. You must always insert this statement in your constructors to make sure that any initialization tasks that must be performed by the base class will not be skipped.
The Contact class’s constructor accepts a single argument: the company name (this property can’t be a blank string). Another useful constructor for the same class accepts two additional arguments, the contact’s first and last names, as follows:
Public Sub New(ByVal CompanyName As String, _
ByVal LastName As String, ByVal FirstName As String)
MyBase.New()
Me.ContactName = LastName & ", " & FirstName
Me.CompanyName = CompanyName
End Sub
Code language: VB.NET (vbnet)
With the two parameterized constructors in place, you can create new instances of the Contact class by using statements such as the following:
Dim contact1 As New Contact("Around the Horn")
Code language: VB.NET (vbnet)
Or the following:
Dim contact1 As New Contact("Around the Horn", "Hardy", "Thomas")
Code language: VB.NET (vbnet)
Notice the lack of the Overloads (or Overrides) keyword. Constructors can have multiple forms and don’t require the use of Overloads — just supply as many implementations of the New() subroutine as you need.
One last, but very convenient technique to initialize objects was introduced with Visual Basic 2008. This technique allows you to supply values for as many properties of the new object as you wish, using the With keyword. The following statements create two new instances of the Person class, and they initialize each one differently:
Dim P1 As New Person With _
{.LastName = "Doe", .FirstName = "Joe"})
Dim P2 As New Person With _
{.LastName = "Doe", .Email = "[email protected]"})
Code language: VB.NET (vbnet)
This syntax allows you to quickly initialize new objects, regardless of their constructors; in effect, you can create your own constructor for any class. This technique will be handy when combining object initialization with other statements, such as in the following example, which adds a new object to a list:
Persons.Add(New Person With {.LastName = "Doe", .FirstName = "Joe"})
Persons.Add(New Person With {.LastName = "Doe"})
Code language: VB.NET (vbnet)