Partager via


Lifetime in Visual Basic 

The lifetime of a declared element is the period of time during which it is available for use. Variables are the only elements that have lifetime. For this purpose, the compiler treats procedure parameters and function returns as special cases of variables. The lifetime of a variable represents the period of time during which it can hold a value. Its value can change over its lifetime, but it always holds some value.

Different Lifetimes

A member variable (declared at module level, outside any procedure) typically has the same lifetime as the element in which it is declared. A nonshared variable declared in a class or structure exists as a separate copy for each instance of the class or structure in which it is declared. Each such variable has the same lifetime as its instance. However, a Shared variable has only a single lifetime, which lasts for the entire time your application is running.

A local variable (declared inside a procedure) exists only while the procedure in which it is declared is running. This applies also to that procedure's parameters and to any function return. However, if that procedure calls other procedures, the local variables retain their values while the called procedures are running.

Beginning of Lifetime

A local variable's lifetime begins when control enters the procedure in which it is declared. Every local variable is initialized to the default value for its data type as soon as the procedure begins running. When the procedure encounters a Dim Statement (Visual Basic) that specifies initial values, it sets those variables to those values, even if your code had already assigned other values to them.

Each member of a structure variable is initialized as if it were a separate variable. Similarly, each element of an array variable is initialized individually.

Variables declared within a block inside a procedure (such as a For loop) are initialized on entry to the procedure. These initializations take effect whether or not your code ever executes the block.

End of Lifetime

When a procedure terminates, the values of its local variables are not preserved, and Visual Basic reclaims their memory. The next time you call the procedure, all its local variables are created afresh and reinitialized.

When an instance of a class or structure terminates, its nonshared variables lose their memory and their values. Each new instance of the class or structure creates and reinitializes its nonshared variables. However, Shared variables are preserved until your application stops running.

Extension of Lifetime

If you declare a local variable with the Static (Visual Basic) keyword, its lifetime is longer than the execution time of its procedure. The following table shows how the procedure declaration determines how long a Static variable exists.

Procedure location and sharing Static variable lifetime begins Static variable lifetime ends

In a module (shared by default)

The first time the procedure is called

When your application stops running

In a class or structure, Shared (procedure is not an instance member)

The first time the procedure is called either on a specific instance or on the class or structure name itself

When your application stops running

In an instance of a class or structure, not Shared (procedure is an instance member)

The first time the procedure is called on the specific instance

When the instance is released for garbage collection (GC)

See Also

Tasks

How to: Lengthen a Variable's Lifetime
Troubleshooting Data Types

Reference

Shared (Visual Basic)
Nothing (Visual Basic)

Concepts

Declared Element Names
Scope in Visual Basic
Access Levels in Visual Basic
Variables in Visual Basic
Variable Declaration in Visual Basic

Other Resources

References to Declared Elements