A local declaration statement declares a new local variable. Its syntax is the same as a data member or constant declaration, with the exception that a local variable declaration must always specify a modifier.
There are three types of locals. Constant locals are equivalent to type member constants and are specified with the Const
modifier. Regular locals are declared using the Dim
modifier. Static locals are declared using the Static
modifier. Static locals are locals that retain their value across invocations of the method. Static locals declared within non-shared methods are per-instance; each instance of the type that contains the method has its own copy of the static local. Static locals declared within shared methods are per-type; there is only one copy of the static local for all instances. It is not legal to put an initializer of any kind on a Static
local.
Variable initializers on locals are equivalent to assignment statements placed at the textual location of the declaration. Thus, if execution branches over the local declaration, the variable initializer will not be executed. If the local declaration is executed more than once, then the variable initializer will be executed an equal number of times. It is important to note that locals are only initialized to their type's default value once, upon entry into the method. Initializers are not allowed on static locals.
Local variables are scoped to the statement block in which they are declared.
Static
| Dim
| Const
As
TypeName ]As
[ New
] TypeName [ (
[ ArgumentList ] )
] ] [ = VariableInitializer ],
Identifier [ ArrayNameModifier ]