Export (0) Print
Expand All

9.3.1 Instance Constructors

Visual Studio .NET 2003

Instance constructors initialize instances of a type and are run by the .NET Framework when an instance is created. The parameter list of a constructor is subject to the same rules as the parameter list of a method. Instance constructors may be overloaded.

All constructors in reference types must invoke another constructor. If the invocation is explicit, it must be the first statement in the constructor method body. The statement can either invoke another of the type's instance constructors — for example, Me.New(...) or MyClass.New(...) — or if it is not a structure it can invoke an instance constructor of the type's base type — for example, MyBase.New(...). It is invalid for a constructor to invoke itself. If a constructor omits a call to another constructor, MyBase.New() is implicit. If there is no parameterless base type constructor, a compile-time error occurs. Because Me is not considered to be constructed until after the call to a base class constructor, the parameters to a constructor invocation statement cannot reference Me, MyClass, or MyBase implicitly or explicitly.

When a constructor's first statement is of the form MyBase.New(...), the constructor implicitly performs the initializations specified by the variable initializers of the instance variables declared in the type. This corresponds to a sequence of assignments that are executed immediately after invoking the direct base type constructor. Such ordering ensures that all base instance variables are initialized by their variable initializers before any statements that have access to the instance are executed. For example:

Imports System

Class A
    Protected x As Integer = 1
End Class

Class B
    Inherits A

    Private y As Integer = x

    Public Sub New()
        Console.WriteLine("x = " & x & ", y = " & y)
    End Sub
End Class

When New B() is used to create an instance of B, the following output is produced:

x = 1, y = 1

The value of y is 1 because the variable initializer is executed after the base class constructor is invoked. Variable initializers are executed in the textual order they appear in the type declaration.

When a type declares only Private constructors, it is not possible in general for other types to derive from the type or create instances of the type; the only exception is types nested within the type. Private constructors are commonly used in types that contain only Shared members.

If a type contains no instance constructor declarations, a default constructor is automatically provided. The default constructor simply invokes the parameterless constructor of the direct base type. If the direct base type does not have an accessible parameterless constructor, a compile-time error occurs. The declared access type for the default constructor is always Public.

In the following example a default constructor is provided because the class contains no constructor declarations:

Class Message
    Private sender As Object
    Private text As String
End Class 

Thus, the example is precisely equivalent to the following:

Class Message
    Private sender As Object
    Private text As String

    Public Sub New()
    End Sub
End Class 

See Also

9.3.2 Shared Constructors | 9.3 Constructors | Using Constructors and Destructors (Visual Basic Language Concepts)

Show:
© 2014 Microsoft