Export (0) Print
Expand All

9.6.3 Variable Initializers

Visual Studio .NET 2003

Instance and shared variable declarations in classes (but not structures) may include variable initializers. For Shared variables, variable initializers correspond to assignment statements that are executed after the program begins, but before the Shared variable is first referenced. For instance variables, variable initializers correspond to assignment statements that are executed when an instance of the class is created. Structures cannot have variable initializers because their parameterless constructors cannot be modified.

Consider the following example:

Imports System

Class Test
    Public Shared x As Double = Math.Sqrt(2.0)
    Public i As Integer = 100
    Public s As String = "Hello"
End Class

Module TestModule
    Sub Main()
        Dim a As Test = New Test()

        Console.WriteLine("x = " & x & ", i = " & a.i & ", s = " & a.s)
    End Sub
End Module

The example produces the following output:

x = 1.414213562373095, i = 100, s = Hello

An assignment to x occurs when the class is loaded, and assignments to i and s occur when a new instance of the class is created.

It is useful to think of variable initializers as assignment statements that are automatically inserted in the block of the type's constructor. The following example contains several instance variable initializers.

Imports System.Collections

Class A
    Private x As Integer = 1
    Private y As Integer = -1
    Private count As Integer

    Public Sub New()
        count = 0
    End Sub

    Public Sub New(n As Integer)
        count = n
    End Sub
End Class

Class B
    Inherits A

    Private sqrt2 As Double = Math.Sqrt(2.0)
    Private items As ArrayList = New ArrayList(100)
    Private max As Integer

    Public Sub New()
        Me.New(100)
        items.Add("default")
    End Sub

    Public Sub New(n As Integer)
        MyBase.New(n – 1)
        max = n
    End Sub
End Class

The example corresponds to the code shown below, where each comment indicates an automatically inserted statement.

Imports System.Collections

Class A
    Private x, y, count As Integer

    Public Sub New()
        MyBase.New ' Invoke object() constructor.
        x = 1 ' This is a variable initializer.
        y = -1 ' This is a variable initializer.
        count = 0
    End Sub

    Public Sub New(n As Integer)
        MyBase.New ' Invoke object() constructor. 
        x = 1 ' This is a variable initializer.
        y = - 1 ' This is a variable initializer.
        count = n
    End Sub
End Class

Class B
    Inherits A

    Private sqrt2 As Double
    Private items As ArrayList
    Private max As Integer

    Public Sub New()
        Me.New(100) 
        items.Add("default")
    End Sub

    Public Sub New(n As Integer)
        MyBase.New(n - 1) 
        sqrt2 = Math.Sqrt(2.0) ' This is a variable initializer.
        items = New ArrayList(100) ' This is a variable initializer.
        max = n
    End Sub
End Class

All variables are initialized to the default value of their type before any variable initializers are executed. For example:

Imports System

Class Test
    Public Shared b As Boolean
    Public i As Integer
End Class

Module TestModule
    Sub Main()
        Dim t As Test = New Test()
        Console.WriteLine("b = " & b & ", i = " & t.i)
    End Sub
End Module

Because b is automatically initialized to its default value when the class is loaded and i is automatically initialized to its default value when an instance of the class is created, the preceding code produces the following output:

b = False, i = 0

Each variable initializer must yield a value of the variable's type or of a type that is implicitly convertible to the variable's type. A variable initializer may be circular or refer to a variable that will be initialized after it, in which case the value of the referenced variable is its default value for the purposes of the initializer. Such an initializer is of dubious value.

There are four forms of variable initializers: regular initializers, array-element initializers, array-size initializers, and object initializers. The first two forms appear after an equal sign that follows the type name, the latter two are part of the declaration itself. Only one form of initializer may be used on any particular declaration.

VariableInitializer ::= RegularInitializer | ArrayElementInitializer

See Also

9.6.3.1 Regular Initializers | 9.6.3.4 Array-Element Initializers | 9.6.3.3 Array-Size Initializers | | 9.6.1 Read-Only Variables | 9.6.2 WithEvents Variables | 9.6 Instance and Shared Variables | 4.6 Accessibility | 4.3.3 Shadowing | Declarations and Constants Summary (Visual Basic Language Reference) | Variables (Visual Basic Language Concepts)

Show:
© 2015 Microsoft