Visual Basic 2005 for Programmers (2nd Edition)

9.4. Default and Parameterless Constructors

If a class does not define constructors, the compiler provides a default constructor. This constructor contains no code (i.e., the constructor body is empty) and takes no parameters. You can also provide a constructorcalled a parameterless constructorthat contains code (but, again, takes no parameters), as we demonstrated in class Time (lines 1315 of Fig. 9.1), and as we will see again in the next example. If you provide any constructors for a class, the compiler will not provide a default constructor for that class.

Common Programming Error 9.2

If Public constructors are provided for a class, but none of them is a parameterless constructor, and an attempt is made to call a constructor with no arguments to initialize an object of the class, a compilation error occurs. A constructor can be called with no arguments only if there are no constructors for the class (in which case the default constructor is called) or if the class includes a parameterless constructor provided by the programmer.

Категории