Visual Basic 2005 with .NET 3.0 Programmer's Reference

When you declare a reference variable, Visual Basic allocates space for the reference. Initially, that reference is set to Nothing, so it doesn’t point to anything and no memory is allocated for an actual object.

You create an object by using the New keyword. Creating an actual object is called instantiating the class.

The following code shows a simple object declaration and instantiation. The first line declares the reference variable. The second line makes the variable point to a new Employee object.

Dim emp As Employee ' Declare a reference to an Employee object. emp = New Employee ' Make a new Employee object and make emp point to it.

Visual Basic also enables you to declare and initialize a variable in a single statement. The following code shows how to declare and initialize an object reference in one statement:

Dim emp As Employee = New Employee ' Declare and instantiate an Employee object.

Visual Basic lets you declare a variable to be of a new object type, as shown in the following statement. This version has the same effect as the previous one but is slightly more compact.

Dim emp As New Employee ' Declare and instantiate an Employee object.

Both of these versions that define and initialize an object in a single statement ensure that the variable is initialized right away. They guarantee that the object is instantiated before you try to use it. If you place these kinds of declarations immediately before the code where the object is used, they also make it easy to see where the object is defined.

Although you can declare and instantiate a reference variable separately, value type variables are allocated when they are declared. Because structures are value types, when you declare one you also allocate space for its data, so you don’t need to use the New keyword to initialize a structure variable.

Both classes and structures can provide special subroutines called constructors. A constructor is a special subroutine named New that Visual Basic calls when a new instance of the class or structure is created. The constructor can perform initialization tasks to get the new object ready for use.

A constructor can optionally take parameters to help in initializing the object. For example, the Person class shown in the following code has a constructor that takes as parameters first and last names and saves them in the control’s FirstName and LastName variables.

Public Class Person Public FirstName As String Public LastName As String Public Sub New(ByVal first_name As String, ByVal last_name As String) FirstName = first_name LastName = last_name End Sub End Class

The following code shows how a program might use this constructor to create a new Person object:

Dim author As New Person("Rod", "Stephens")

You can overload the New method just as you can overload other class methods. The different overloaded versions of the constructor must have different parameter lists so that Visual Basic can decide which one to use when it creates a new object.

The following code shows a Person class that provides two constructors. The first takes no parameters and sets the object’s FirstName and LastName values to <unknown>. The second version takes two strings as parameters and copies them into the object’s FirstName and LastName values.

Public Class Person Public FirstName As String Public LastName As String Public Sub New() Me.New("<unknown>", "<unknown>") End Sub Public Sub New(ByVal first_name As String, ByVal last_name As String) FirstName = first_name LastName = last_name End Sub End Class

The following code uses each of these constructors:

Dim person1 As New Person ' <unknown> <unknown>. Dim person2 As New Person("Olga", "O'Toole") ' Olga O'Toole.

If you do not provide any constructors for a class, Visual Basic allows the program to use the New keyword with no parameters. If you create any constructor, however, Visual Basic does not allow the program to use this default empty constructor (without parameters) unless you build one explicitly. For example, if the previous version of the Person class did not include an empty constructor, the program could not use the first declaration in the previous code that doesn’t include any parameters.

You can use this feature to ensure that the program assigns required values to an object. In this case, it would mean that the program could not create a Person object without assigning FirstName and LastName values.

If you want to allow an empty constructor in addition to other constructors, an alternative is to create a single constructor with optional parameters. The following code shows this approach. With this class, the program could create a new Person object, passing its constructor zero, one, or two parameters.

Public Class Person Public FirstName As String Public LastName As String Public Sub New( _ Optional ByVal first_name As String = "<unknown>", _ Optional ByVal last_name As String = "<unknown>") FirstName = first_name LastName = last_name End Sub End Class

Категории