Scope comes in two flavors, local and global. Local scope means that a variable can be accessed only by code within a single procedure - the one in which it was declared.

Global scope means that a variable can be accessed by code in any procedure within a PowerBASIC program.

Where a variable declaration is made determines the scope that a variable will exhibit. Declarations made within a procedure result in local scope, while declarations made outside procedures result in global scope.

When a procedure is closed the local variables are destroyed and their values lost (unless STATIC was used to declare the variables). Global variables are not destroyed until the application closes.

Here are the PowerBASIC variable declaration statements.

And here are the allowed locations of declaration statements within PowerBASIC programs. You can see from this listing that only Global and Threaded declaration statements result in global variables. Note also that the PowerBASIC compiler allows the CLASS/Sub/Function code blocks to be placed before or after the PBMain() function.


    GLOBAL/THREADED ...                         'global declarations

    Function PBMain() As Long
        DIM/REDIM/LOCAL/STATIC ...              'local declarations
    End Function

    CLASS myClass
        INSTANCE                                'local declarations
        INTERFACE myInterface
            METHOD myMethod
                DIM/REDIM/LOCAL/STATIC...       'local declarations
            END METHOD
            PROPERTY myProperty
                DIM/REDIM/LOCAL/STATIC...       'local declarations
            END PROPERTY

    SUB mySub() 
        DIM/REDIM/LOCAL/STATIC ...              'local declarations

    FUNCTION myFunction() 
        DIM/REDIM/LOCAL/STATIC ...               'local declarations

Note also that while the code that defines a CLASS is placed outside all procedures, object variable definition occurs in the same locations as all other variable types.

See the declarations tutorial for syntax and examples on using the declare statements. Usage is very straight-forward but be sure to note that when using the GLOBAL declaration on arrays, the array variables must be declared with no subscripts.

Local Scope
When a variable declaration statement is used inside a procedure (Sub/Function/Method/Property), the variable may be accessed only by code within that procedure.

This is called local scope.

When a procedure ends any local variables created within the procedure are destoryed and their values lost.

Locally scoped variable, those declared inside procedures, can use only DIM/REDIM/LOCAL/STATIC/INSTANCE declaration statements. Their positions within a PowerBASIC application are shown in the listing above.

PowerBASIC also allows declaring variables which can be accessed anywhere in a program. This is know as global scope.

Global Scope
Variable declaration statements can also be placed outside procedures, typically at the top of the source code file before any procedures are defined.

These variables can be accessed anywhere in the program, particularly inside of all procedures.

This is called global scope.

Only GLOBAL/THREADED declaration statements can be used to create variables with global scope. Their positions within a PowerBASIC application are shown in the listing above.

If you have any suggestions or corrections, please let me know.