Getting Started
Introduction
Sample Programs
IDEs
History
Advice
Mini-Tutorial
Tutorials
Code Snippets

Resources
Web Sites
More Tutorials
Forums
Vendors/Tools
Books
Magazines
Newsletters
NewsGroups
User Groups
Talk Shows
Blogs

Controls
Overview
Button
Check3State
Checkbox
ComboBox
Frame
Graphic
Image
ImageX
ImgButton
ImgButtonX
Label
Line
ListBox
ListView
Option
Progress Bar
Scrollbar
StatusBar
TAB
TextBox
Toolbar
TreeView

GBIC >> PowerBASIC >> Tutorials >> Scope

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • Scope
    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.

      • Local Scope  
       Dim, ReDim, Local, Static, Instance 
      • Global Scope
       Global, Threaded 

    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.

        #COMPILE EXE
    
        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
            END INTERFACE
        END CLASS
    
        SUB mySub() 
            DIM/REDIM/LOCAL/STATIC ...              'local declarations
        END SUB
    
        FUNCTION myFunction() 
            DIM/REDIM/LOCAL/STATIC ...               'local declarations
        END SUB
    

    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.