Variable Scope

The scope of a variable is defined as its visibility and its lifetime. Visibility means what parts of your program can access it. Lifetime defines when it is created and when it is destroyed. In PowerBASIC, there are many choices of scope to afford the maximum flexibility. You may choose any scope which best fits the needs of your program. When any variable is created in PowerBASIC, it is automatically initialized. Numeric variables are initialized to zero (0). Dynamic strings, Field strings, and ASCIIZ strings are initialized to a length of zero (no characters). Fixed-length strings and UDTs are filled with CHR$(0). PowerBASIC automatically destroys every variable when at the appropriate time, so you never need worry about this type of memory leak.

 

LOCAL

Local variables are only accessible within a single SUB, FUNCTION, METHOD, or PROPERTY. They are automatically created and initialized each time you enter the procedure. They are automatically destroyed when you exit. This is the default variable scope unless you declare otherwise.

STATIC

Static variables are only accessible within a single SUB, FUNCTION, METHOD, or PROPERTY. They are initialized when your program starts, but retain their value regardless of how many times the procedure is entered and exited. They are destroyed only when the program ends.

GLOBAL

Global variables are accessible from anywhere in your program. They are initialized when your program starts and are destroyed when the program ends.

THREADED

Threaded variables are accessible from anywhere in your program, but each thread within your program will have its own unique copy of them. They are created and initialized when a thread is created. They are destroyed when the thread ends. Threaded variables are commonly called Thread Local Storage (TLS).

INSTANCE

Instance variables are accessible from any method or property in a class. Each object will have its own unique copy of them. They are created and initialized when an object is created. They are destroyed when an object is destroyed.

 

See Also

Variables

Default Variable Typing

THREADED variables

LOCAL, GLOBAL and STATIC considerations