Visual Basic 2005 for Programmers (2nd Edition)

4.7. Initializing Objects with Constructors

As mentioned in Section 4.5, when an object of class GradeBook (Fig. 4.7) is created, its instance variable courseNameValue is initialized to Nothing by default. What if you want to provide a course name when you create a GradeBook object? Each class you declare can provide a constructor that can be used to initialize an object of the class when the object is created. In fact, Visual Basic requires a constructor call for every object that is created. The New keyword calls the class's constructor to perform the initialization. The constructor call is indicated by the class name followed by parentheses. For example, line 8 of Fig. 4.8 first uses New to create a GradeBook object. The lack of parentheses after "New GradeBook" indicates a call to the class's constructor that takes no arguments. By default, the compiler provides a default constructor with no parameters in any class that does not explicitly include a constructor.

When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class. For example, a programmer might want to specify a course name for a GradeBook object when the object is created, as in

Dim gradeBook As New GradeBook( _ "CS101 Introduction to Visual Basic Programming")

This passes the argument "CS101 Introduction to Visual Basic Programming" to the GradeBook object's constructor, which uses the argument value to initialize instance variable courseNameValue. The preceding statement requires that the class provide a constructor with a String parameter. Fig. 4.12 contains a modified GradeBook class with such a constructor.

Figure 4.12. GradeBook class with a constructor that receives a course name.

1 ' Fig. 4.12: GradeBook.vb 2 ' GradeBook class with a constructor to initialize the course name. 3 Public Class GradeBook 4 Private courseNameValue As String ' course name for this GradeBook 5 6 ' constructor initializes course name with String supplied as argument 7 Public Sub New (ByVal name As String) 8 CourseName = name ' initialize courseNameValue via property 9 End Sub ' New 10 11 ' property CourseName 12 Public Property CourseName() As String 13 Get ' retrieve courseNameValue 14 Return courseNameValue 15 End Get 16 17 Set (ByVal value As String) ' set courseNameValue 18 courseNameValue = value ' store the course name in the object 19 End Set 20 End Property ' CourseName 21 22 ' display a welcome message to the GradeBook user 23 Public Sub DisplayMessage() 24 ' use property CourseName to display the 25 ' name of the course this GradeBook represents 26 Console.WriteLine("Welcome to the grade book for " _ 27 & vbCrLf & CourseName & "!") 28 End Sub ' DisplayMessage 29 End Class ' GradeBook

Lines 79 declare the constructor for class GradeBooka constructor must have the name New. As with the other methods we have declared, a constructor specifies in its parameter list the data it requires to perform its task. When you create a new object, this data is placed in the parentheses that follow the class name (as in lines 811 of Figure 4.13). Line 7 (Fig. 4.12) indicates that class GradeBook's constructor has a parameter called name of type String.

Figure 4.13. Constructor used to initialize GradeBook objects.

1 ' Fig. 4.13: GradeBookTest.vb 2 ' GradeBook constructor used to specify the course name at the 3 ' time each GradeBook object is created. 4 Module GradeBookTest 5 ' Main begins program execution 6 Sub Main() 7 ' create GradeBook object 8 Dim gradeBook1 As New GradeBook( _ 9 "CS101 Introduction to Visual Basic Programming") 10 Dim gradeBook2 As New GradeBook( _ 11 "CS102 Data Structures in Visual Basic") 12 13 ' display initial value of CourseName for each GradeBook 14 Console.WriteLine( _ 15 "gradeBook1 course name is: " & gradeBook1.CourseName) 16 Console.WriteLine( _ 17 "gradeBook2 course name is: " & gradeBook2.CourseName) 18 End Sub ' Main 19 End Module ' GradeBookTest

[View full width]

gradeBook1 course name is: CS101 Introduction to Visual Basic Programming gradeBook2 course name is: CS102 Data Structures in Visual Basic

Line 8 of the constructor's body assigns name to property CourseName. This causes the Set accessor of property CourseName to execute, which (in line 18) assigns parameter value to instance variable courseNameValue. You might be wondering why we bother using property CourseNamethe constructor certainly could perform the assignment courseNameValue = name. In Section 4.8, we will modify the Set accessor of property CourseName to perform validation (in this case, to ensure that the courseNameValue is 25 or fewer characters in length). At that point the benefits of using property CourseName from the constructor will become clear.

Figure 4.13 demonstrates initializing GradeBook objects using this constructor. Lines 89 create and initialize a GradeBook object. The constructor of class GradeBook is called with the argument "CS101 Introduction to Visual Basic Programming" to initialize the course name. The object-creation expression in lines 89 returns a reference to the new object, which is assigned to variable gradeBook1. Lines 1011 repeat this process for another GradeBook object, this time passing the argument "CS102 Data Structures in Visual Basic" to initialize the course name for gradeBook2. Lines 1417 use each object's CourseName property to display the course names and show that they were initialized properly when the objects were created. The output confirms that each GradeBook maintains its own copy of instance variable courseNameValue.

Like other methods, constructors also can take arguments. However, an important difference between constructors and methods is that constructor declarations cannot return values (even though, as you know, each constructor does return a referenceto an object of its class type). Normally, constructors are declared Public. If a class does not include a constructor, the class's instance variables are initialized to their default values.

Error-Prevention Tip 4.1

Unless default initialization of your class's instance variables is acceptable, provide a constructor to ensure that these variables are properly initialized with meaningful values when each new object of your class is created.

Adding the Constructor to Class GradeBook's UML Class Diagram

The UML class diagram of Fig. 4.14 models the class GradeBook of Fig. 4.12, which has a constructor that has a name parameter of type String. Like operations, the UML models constructors in the third compartment of a class in a class diagram. To distinguish a constructor from a class's operations, the UML places the word "constructor" between guillemets (« and ») before the constructor's name (New). It is customary to list constructors before other operations in the third compartment.

Figure 4.14. UML class diagram indicating that class GradeBook has a constructor that has a name parameter of type String.

Категории