GLOBAL

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

The variables listed in a GLOBAL statement are visible to all parts of a program, including subs and functions as well as the main program.
See also, Variables and ByRef.

Syntax

  1. global var1, var2, var3,...

Hints

Global variables are subject to being changed when within subs or functions. This can happen accidentally when variables are declared global. Be certain to watch the state of variables in this instance in order to avoid unexpected results, which are sometimes very hard to isolate.
If your sub or function uses a global variable, but you do not want the GLOBAL value changed, make a "copy" of the variable with a unique name for use in the sub or function. ie: uniqueNameVar=global var1.
These variables are global by default:
The special system variables WindowWidth, WindowHeight, UpperLeftX, UpperLeftY, ForegroundColor$, BackgroundColor$, ListboxColor$, TextboxColor$, ComboboxColor$, TexteditorColor$, DefaultDir$, Joy1x, Joy1y, Joy1z, Joy1button1, Joy1button2, Joy2x, Joy2y, Joy2z, Joy2button1, Joy2button2, and Com now have true global status.
Arrays and windows and controls which are identified by #handle are all globally visible.
Also, it appears that GLOBAL is a compiler directive. Whether a variable is global or not in a function/sub seems to depend only on whether the global declaration has appeared earlier in the code or not.
That means that
  • it does not have to be executed (and you can not disable it with IF)
  • you better put GLOBAL in the program's top. Chances are, that placed in sub, it will not work

Example

' define a global string variable:
    global title$
    title$ = "Great Program!"

' Special system variables don't
'  need to be declared as global,
'  since they have that status automatically
BackgroundColor$ = "darkgray"
ForegroundColor$ = "darkblue"

' call my subroutine to open a window
    call openIt
    wait

sub openIt
    statictext #it.stext, "Look Mom!", 10, 10, 70, 24
    textbox #it.tbox, 90, 10, 200, 24
    open title$ for window as #it
    print #it.tbox, "No hands!"

end sub

Useful Procedures

' Place a useful function or sub using this keyword here