Share via


How to: Lengthen a Variable's Lifetime

Normally, a variable ceases to exist when the programming element in which it is declared ceases to exist. However, you can make its lifetime longer than that of its containing element by declaring it with the Static (Visual Basic) keyword.

For more information, see Lifetime in Visual Basic.

To make a variable live longer than its containing element

  • If the variable is a local variable (declared inside a procedure), include the Static keyword in the Dim Statement (Visual Basic) that declares it. A static variable exists as long as the class or module containing the procedure that declares the variable.

    If a local variable is inside a Shared (Visual Basic) procedure, the procedure and its variables exist as long as your application runs. Do not use Static in this case.

  • If the variable is a member variable (declared in a class or structure, outside any procedure), include the Shared keyword in the Dim statement that declares it. A shared variable is not associated with any one instance of its class or structure, and it exists as long as your application runs. You cannot use Static on a member variable.

    If a member variable is declared in a module, it always exists as long as your application runs. Do not use Shared in this case.

Example

The following example declares a variable with the Static (Visual Basic) keyword. (Note that you do not need the Dim keyword when the Dim Statement (Visual Basic) uses a modifier such as Static.)

Function runningTotal(ByVal num As Integer) As Integer
    Static applesSold As Integer
    applesSold = applesSold + num
    Return applesSold
End Function

In the preceding example, the variable applesSold continues to exist after the procedure runningTotal returns to the calling code. The next time runningTotal is called, applesSold retains its previously calculated value.

If applesSold had been declared without using Static, the previous accumulated values would not be preserved across calls to runningTotal. The next time runningTotal was called, applesSold would have been recreated and initialized to 0, and runningTotal would have simply returned the same value with which it was called.

Compiling the Code

You can initialize the value of a static local variable as part of its declaration. If you declare an array to be Static, you can initialize its rank (number of dimensions), the length of each dimension, and the values of the individual elements.

Static Variables of the Same Name

You can declare static variables with the same name in more than one procedure. If you do this, the Visual Basic compiler considers each such variable to be a separate element. The initialization of one of these variables does not affect the values of the others. The same applies if you define a procedure with a set of overloads and declare a static variable with the same name in each overload.

Containing Elements for Static Variables

You can declare a static local variable within a class, that is, inside a procedure in that class. However, you cannot declare a static local variable within a structure, either as a structure member or as a local variable of a procedure within that structure.

Security

In the preceding example, you can produce the same lifetime by declaring applesSold at module level. If you changed the scope of a variable this way, however, the procedure would no longer have exclusive access to it. Because other procedures could access applesSold and change its value, the running total could be unreliable and the code could be more difficult to maintain.

See Also

Concepts

Lifetime in Visual Basic

Declared Element Characteristics

Scope in Visual Basic

Access Levels in Visual Basic

Variables in Visual Basic

Variable Declaration in Visual Basic

Procedure Overloading