Keyword

Static


Description

Creates a local variable or local array with the name and size (in the case of an array) and data type specified. A variable declared with the Static statement and assigned a value retains its value from one invocation of the method to the next. In contrast, variables declared with the Var statement are completely local to the method and are destroyed when each invocation of the method goes out of scope.

Usage

The Static statement has two syntaxes:

Static variableName [ , variableNameN ] As [ New ] DataType [ = InitialValue ]

Part

Type

Description

variableName

Variable name

The name of the new variable.

variableNameN

Variable name

Optional. The names of other variables you wish to create with the same data type.

DataType

Data type name

The data type of the variable.

InitialValue

Same type as DataType

Initial value for variableName. If the datatype is an object that requires instantiation via the New operator, you can do that instead of assigning an initial value.

Static arrayName(size [ , sizeN ]) As DataType

Part

Type

Description

arrayName

Variable name

The name of the new array.

size

Integer

The size (number of elements) for the array. Arrays are zero-based.

sizeN

Integer

Optional. The size of the next dimension of the array if you are creating a multi-dimensional array.

DataType

Data type name

The data type of the array.

Notes

A Static variable is equivalent to a local variable declared by the Var statement but its value is retained between several calls of the method it is declared in. Also, all the instances of a given class share the same Static values. .. note:: Whenever you assign an initial value to a Static variable using the equal sign, the assignment will occur only once when the method is called for the very first time.

For example, if you want a serial number to be generated, i.e. a method with always returns a new increasing value, the simplest is to use a Static value as:

Function SerialNumber() As Integer
  Static currentSerialNumber As Integer = 0

  currentSerialNumber = currentSerialNumber + 1 ' Increment the value
  Return currentSerialNumber ' Return the value
End Function

You can also use Static to cache a value which takes time to get so you can improve the performance of your code:

Computed Property MyCachedProperty
Get
  ' Inside the Get part
  Static theProperty As String = AMethodWhichTakesSomeTime ' This will be executed only the first time

  Return theProperty
End Get

Sample code

This example uses the Static statement to create an Integer variable called "age" and assigns it the value 33.

Static age As Integer = 33

This example uses the Static statement to create two string variables.

Static FirstName, LastName As String

This example uses the Static statement to create an array called aNames with 11 elements (remember, arrays have a zero element).

Static aNames(10) As String

This example uses the Static statement to create an array called aNames with one element.

Static aNames(0) As String

Compatibility

All project types on all supported operating systems.

See also

Arrays concept; Collection, Dictionary classes; Var statement.