xHarbour Reference Documentation > Statement Reference |
Declares and optionally initializes a GLOBAL memory variable.
GLOBAL <varName> [:= <xValue> ] GLOBAL EXTERNAL <extVarName>
The GLOBAL statement declares a memory variable that has GLOBAL scope. Global variables are resolved by the compiler, i.e. their symbolic name cannot be retrieved during runtime. This makes access to GLOBAL variables much faster than to memory variables of PRIVATE or PUBLIC scope, whose symbolic variable names exist at runtime.
The names of GLOBAL variables cannot be included in macro-expressions since they cannot be resolved by the macro operator (&). This operator requires the symbolic name of a variable to exist at runtime.
The lifetime of GLOBAL variables is identical with the lifetime of a program. They are visible and accessible throughout the functions and procedures of all PRG files that declare or refer to global variables.
Variable of other types cannot have the same symbolic name as a GLOBAL variable since a global variable's name must be unique for all modules accessing such a variable. In addition, a GLOBAL can only be declared in one PRG file using the GLOBAL statement. When a global variable must be accessed in another PRG file, it must be referred to using the GLOBAL EXTERNAL statement.
See also: | LOCAL, MEMVAR, PRIVATE, PUBLIC, STATIC |
Category: | Declaration , Statements , xHarbour extensions |
// The example demonstrates how to declare/initialize a global variable // in one file, while it is referred to in a second file. ** Global1.prg GLOBAL gName := "My Global" PROCEDURE Main ? gName // result: My Global Test() ? gName // result: New Name RETURN ** Global2.prg GLOBAL EXTERNAL gName PROCEDURE Test gName := "New Name" RETURN
http://www.xHarbour.com