Difference between revisions of "Static"
From Xojo Documentation
|Line 115:||Line 115:|
== Sample Code ==
== Sample Code ==
This example uses the '''Static''' statement to create an [[Integer
This example uses the '''Static''' statement to create an [[Integer]] variable called "age" and assigns it the value 33.
Latest revision as of 22:40, 1 April 2021
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]
|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
|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:
Static currentSerialNumber As integer = 0
currentSerialNumber = currentSerialNumber + 1 // Increment the value
Return currentSerialNumber // Return the value
You can also use Static to cache a value which takes time to get so you can improve the performance of your code:
// Inside the Get part
Static theProperty As String = AMethodWhichTakesSomeTime() // This will be executed only the first time
This example uses the Static statement to create an Integer variable called "age" and assigns it the value 33.
This example uses the Static statement to create two string variables.
This example uses the Static statement to create an array called aNames with 11 elements (remember, arrays have a zero element).
This example uses the Static statement to create an array called aNames with one element.