Export (0) Print
Expand All

10.2 Local Declaration Statements

Visual Studio .NET 2003

A local declaration statement declares a new local variable, local constant, or static variable. Local variables and local constants are equivalent to instance variables and constants scoped to the method and are declared in the same way. Static variables are similar to Shared variables and are declared using the Static modifier.

Static variables are locals that retain their value across invocations of the method. Static variables declared within nonshared methods are per instance: each instance of the type that contains the method has its own copy of the static variable. Static variables declared within Shared methods are per type; there is only one copy of the static variable for all instances. While local variables are initialized to their type's default value upon each entry into the method, static variables are only initialized to their type's default value when the type or type instance is initialized. Static variables may not be declared in structures.

Local variables, local constants, and static variables always have public accessibility and may not specify accessibility modifiers. If no type is specified on a local declaration statement and strict semantics are being used, a compile-time error occurs. Otherwise the type of the property is implicitly Object or the type of the property's type character.

Variable initializers on local declaration statements are equivalent to assignment statements placed at the textual location of the declaration. Thus, if execution branches over the local declaration statement, the variable initializer is not executed. If the local declaration statement is executed more than once, the variable initializer is executed an equal number of times. Static variables only execute their initializer the first time. If an exception occurs while initializing a static variable, the static variable is considered initialized with the default value of the static variable's type.

The following example shows the use of initializers:

Imports System

Module Test
    Sub F()
        Static x As Integer = 5

        Console.WriteLine("Static variable x = " & x)
        x += 1
    End Sub

    Sub Main()
        Dim i As Integer

        For i = 1 to 3
        Next i

        i = 3
        Dim y As Integer = 8

        If i > 0 Then
            Console.WriteLine("Local variable y = " & y)
            y -= 1
            i -= 1
            Goto label
        End If
    End Sub

This program prints:

Static variable x = 5
Static variable x = 6
Static variable x = 7
Local variable y = 8
Local variable y = 8
Local variable y = 8

Local variables, local constants, and static variables are scoped to the statement block in which they are declared. Static variables are special in that their names may only be used once throughout the entire method. For example, it is not valid to specify two static variable declarations with the same name even if they are in different blocks.

LocalDeclarationStatement ::=
   LocalModifier LocalDeclarators StatementTerminator
LocalModifier ::= Static | Dim | Const
LocalDeclarators ::=
   LocalDeclarator | 
   LocalDeclarators , LocalDeclarator
LocalDeclarator ::=
   LocalIdentifiers [ As TypeName ] |
   LocalIdentifier [ As [ New ] TypeName [ ( [ ArgumentList ] ) ] ]
      [ = VariableInitializer ]
LocalIdentifiers ::=
   LocalIdentifier |
   LocalIdentifiers , LocalIdentifier
LocalIdentifier ::= Identifier [ ArrayNameModifier ]

See Also

10.1 Blocks | 10.1.1 Local Variables and Parameters | 10.3 With Statement | 10.4 SyncLock Statement | 10.5 Event Statements | 10.6 Assignment Statements | 10.7 Invocation Statements | 10.8 Conditional Statements | 10.9 Loop Statements | 10.10 Exception-Handling Statements | 10.11 Branch Statements | 10.12 Array-Handling Statements | Declaration Statements (Visual Basic Language Concepts)

© 2015 Microsoft