4.7. Initializing Objects with ConstructorsAs 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.
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.
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
Adding the Constructor to Class GradeBook's UML Class DiagramThe 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.
|