From Xojo Documentation

Revision as of 22:40, 1 April 2021 by Gperlman (talk | contribs) (Sample Code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Language Keyword

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.


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.

Each name should be separated with a comma.

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.


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.

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
// 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
age = 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

See Also

Arrays concept; Collection, Dictionary classes; Var statement.