GLOBAL statement   

Purpose

Declare global (shared) variables between Subs, Functions. Methods, and Properties.

Syntax

GLOBAL variable[()] [ AS type] [, variable[()]] [, ...]

GLOBAL variable[()] [, variable[()]] [, ...] AS type

Remarks

GLOBAL declares the specified variable(s) as global to the entire program.  This gives a procedure access to variable(s), without having to pass them as parameters.  To declare an array as a global variable, use an empty set of parentheses in the variable list:

GLOBAL MyArray%()

GLOBAL StringArray() AS STRING

You must then use the DIM or REDIM statements to dimension the array inside a procedure.  A good place to do this is inside your WINMAIN or PBMAIN function.

If an array is defined as GLOBAL outside a procedure, you should include the GLOBAL keyword in the DIM statement for clarity, and compatibility with future versions of PowerBASIC:

GLOBAL a() AS STRING

FUNCTION PBMAIN

  DIM a(1 TO 500) AS GLOBAL STRING

  [statements]

END FUNCTION

The GLOBAL statement may accept a list of variables, all of which are defined by the type descriptor keywords which follow them.  For example:

GLOBAL aaa, bbb, ccc AS INTEGER

GLOBAL vptr, aptr() AS LONG PTR

Restrictions

GLOBAL variables are not shared between programs and DLLs or between multiple instances of the same DLL.  That is, a GLOBAL variable is only global within its own module.  The simplest way to expose a variable to a DLL is to pass the variable BYREF (by reference) to the target DLL. DEFtype has no effect on variables defined by a GLOBAL statement.

See also

DIM, INSTANCE, LOCAL, STATIC, THREADED

Example

#COMPILE EXE

GLOBAL Caption AS ASCIIZ * 255

 

FUNCTION PBMAIN() AS LONG

  DIM Msg AS ASCIIZ * 255

  CALL SetVars

  IF Caption = "GLOBAL test" then Msg = "Success!"

END FUNCTION

 

SUB SetVars()

  Caption = "GLOBAL test"

END SUB