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 ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.