Constructors
In that example, you will create a new instance of the Time class.
Dim timeObject As New Time();
The parentheses look a lot like a method call. In fact, a method is invoked whenever you instantiate an object. This method is called a constructor. If you don't define one as part of your class definition, the compiler will provide one on your behalf.
The job of a constructor is to create the object specified by a class and to put it into a valid state. Before the constructor runs, the object is just a blob of memory; after the constructor completes, the memory holds a valid instance of the class.
Tip
Any constructor that takes no arguments is called a default constructor. It turns out that the constructor provided by the compiler takes no arguments, and, hence, is a default constructor. This terminology has caused a great deal of confusion. You can create your own default constructor, and if you do not create a constructor at all, the compiler will create a default constructor for you, by default.
If you do not explicilty initialize your member variables, they are initialized to their default values based on their type (integers to 0, strings to the empty string, etc.). Table 18-2 lists the default values assigned to various types.
Table 18-2. Types and their default values
|
Type |
Default value |
|---|---|
|
|
|
|
|
|
|
|
The Unicode character whose code point is |
|
|
|
|
|
Nothing |
Typically, you'll want to define your ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access