xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

GLOBAL

Declares and optionally initializes a GLOBAL memory variable.

Syntax

GLOBAL <varName> [:= <xValue> ]
GLOBAL EXTERNAL <extVarName>

Arguments

GLOBAL <varName>
<varName> is the symbolic name of the global variable to declare.
<xValue>
<xValue> is an optional value to assign to the GLOBAL variable after being declared. To assign a value, the inline assignment operator (:=) must be used. The simple assignment operator (=) cannot be used. Only literal values are allowed for <xValue> when declaring GLOBAL variables.
GLOBAL EXTERNAL <extVarName>
This is the name of a GLOBAL variable that is declared in another PRG file and must be referred to in the current PRG file.

Description

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.

Info

See also:LOCAL, MEMVAR, PRIVATE, PUBLIC, STATIC
Category: Declaration , Statements , xHarbour extensions

Example

// 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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe