Maptitude GISDK Help

Variable Scope

 

There are four types of variable scope: local, static, shared, and global.

 

Local Scope

Unless otherwise declared, a variable is local to the GISDK macro or dialog box in which you use it and does not retain its value the next time that GISDK macro or dialog box is called. If you use the same variable name in two GISDK macros, changes to the variable in one macro do not affect the variable in the other:

 

Macro "first macro"

     x = 5

     RunMacro("second macro")

     // x is still 5

endMacro

 

Macro "second macro"

     x = 10

endMacro

 

Variables that are local to a dialog box are available in all dialog box items within the dialog box.

 

Static Scope

Variables can be declared as static using the static statement:

 

static variable, variable, ...

 

A static variable is like a local variable, but will retain its value the next time that GISDK macro or dialog box is called. If the same variable name is declared as static in a different GISDK macro or dialog box, that variable will have an independent value.

 

Shared Scope

You can declare variables to be shared using the shared statement:

 

shared variable, variable, ...

 

A shared variable is shared only in those GISDK macros or dialog boxes in which it appears in a shared statement. You can use shared variables to communicate values between GISDK macros:

 

Macro "first macro"

     shared user_value

     user_value = 5

     RunMacro("second macro")

     RunMacro("third macro")

     // user_value is now 10, but not 20

endMacro

 

Macro "second macro"

     shared user_value

     user_value = 10

endMacro

 

Macro "third macro"

     user_value = 20

endMacro

 

Unlike local variables, a shared variable does not lose its value when a GISDK macro terminates. As such, it can be used in addition to static variables as a way to remember values between invocations of a GISDK macro:

 

Macro "preferences" (pref)

     shared user_pref

     if user_pref <> pref then

          // preference changed; remember it for next time.

          user_pref = pref

endMacro

 

Global Scope

The use of the global statement should be avoided. The use of global variables is considered bad practice and can have major consequences. A global variable will override any local, static, or shared variable of the same name, in any GISDK resource (including those in the standard UI), since that variable will be available in all GISDK macros, menus, objects, and dialog boxes, including toolbars and toolboxes. Shared variables are used in the standard UI when necessary, instead of global variables. The use of global variables rather than arguments to pass values between GISDK functions is particularly discouraged. Debugging your code is much more difficult when you use global variables.

 

If you really must declare variables as global, use the global statement:

 

global variable, variable, ...

 

Please follow these rules when using global variables:

 

 

©2025 Caliper Corporation www.caliper.com