Global
DescriptionGlobal[.<type>] <variable[.<type>]> [= <expression>] [, ...]
Global provides the ability for variables to be defined as global, i.e., variables defined as such may then be accessed within a Procedure. In this case the command Global must be called for the according variables, before the declaration of the procedure. This rule is true everywhere except in a single case: The modules do not have access to the global declared variables outside this module. Each variable may have a default value directly assigned to it. If a type is specified for a variable after Global, the default type is changed through the use of this declaration. Global may also be used with arrays, lists and maps.
In order to use local variables within a procedure, which have the same name as global variables, take a look at the Protected and Static keywords.
Example: With variables
Global a.l, b.b, c, d = 20 Procedure Change() Debug a ; Will be 10 as the variable is global EndProcedure a = 10 Change()
Example: With array
Global Dim Array(2) Procedure Change() Debug Array(0) ; Will be 10 as the array is global EndProcedure Array(0) = 10 Change()
Example: With default type
; 'Angle' and 'Position' will be a float, as they didn't have a specified type ; Global.f Angle, Length.b, Position
Example: Complex with a module
Global Var_GlobalOutsideModule = 12
Debug Var_GlobalOutsideModule ; Display 12
DeclareModule Ferrari
Global Var_GlobalModule = 308
#FerrariName$ = "458 Italia"
Debug #FerrariName$ ; Display "458 Italia"
; Debug Var_GlobalOutsideModule ; Error, the variable already exists
Debug Var_GlobalModule ; Display 308
Declare CreateFerrari()
EndDeclareModule
; Private
; ---------------------------------------------------------------------------------------
Module Ferrari
Debug Var_GlobalOutsideModule ; Display 0 <== Look !
Debug Var_GlobalModule ; Display 308
Global Initialized = 205
Debug Initialized ; Display 205
Procedure Init()
Debug Var_GlobalOutsideModule ; Display 0
Debug Var_GlobalModule ; Display 308
Debug "InitFerrari()"
EndProcedure
Procedure CreateFerrari() ; Public
Init()
Debug "CreateFerrari()"
Debug Var_GlobalOutsideModule ; Display 0
Debug Var_GlobalModule ; Display 308
EndProcedure
EndModule
; Main Code
; ---------------------------------------------------------------------------------------------
Procedure Init()
Debug " init() from main code."
Debug Var_GlobalOutsideModule ; Display 12
Debug Var_GlobalModule ; Display 0
EndProcedure
Init()
Ferrari::CreateFerrari()
Debug Ferrari::#FerrariName$ ; Display 458 Italia
Debug Var_GlobalOutsideModule ; Display 12
; Debug Var_GlobalModule ; Error, the variable already exists
UseModule Ferrari
CreateFerrari()
Debug #FerrariName$ ; Display 458 Italia
Debug Var_GlobalOutsideModule ; Display 12
Debug Var_GlobalModule ; Display 308 <== Look !
UnuseModule Ferrari
; Debug #FerrariName$ ; Error, does not exist
Debug Var_GlobalOutsideModule ; Display 12
Debug Var_GlobalModule ; Display 0 <== Look !