Global vs. Local Variables

InstallShield 2013 » InstallScript Language Reference

Variables may be either global or local:

A variable is global if it is declared outside of the main program block and not within a function. Global variables are visible and available to all statements in a setup script that follow its declaration.
A variable is local if it is declared between the function declaration and the keyword begin within that function. Local variables are visible and available only within the function where they are declared.

Project: InstallScript events are not used in Basic MSI and merge module projects; therefore, all InstallScript code for these project types must be written in InstallScript custom actions. Global variables do not share state between these custom action invocations.

Note: InstallScript system variables are global and therefore are visible to the main program and to all functions in a script.

In the following example, the variable nVisibleEverywhere can be referenced by any statement in the script. The variable nVisibleOnlyToFunctions may be referenced only by the functions. The variable nVisibleOnlyToSecondFunction cannot be referenced by the main program or by FirstFunction. The variable szString is local to FirstFunction.

    prototype FirstFunction();

    prototype SecondFunction();

    

    NUMBER nVisibleEverywhere;

    

    . . .

    

    nVisibleEverywhere = 10;

    

    FirstFunction();

    SecondFunction();

    

    . . .

    

    NUMBER nVisibleOnlyToFunctions;

    

function FirstFunction()

  STRING szString;

begin

  szString = "Local to FirstFunction";

  nVisibleOnlyToFunctions = 20;

end;

 

    NUMBER nVisibleOnlyToSecondFunction;

 

function SecondFunction()

begin

  nVisibleOnlyToSecondFunction = 30;

end;

Although identifiers in a script must be unique, it is valid for a local variable to have a name identical to that of a global variable, or for one function to declare a local variable that has the same name as a local variable declared in another function. These exceptions are allowed because InstallShield qualifies local variable names based on the function with which they are associated. In the example below, the global variable szVal is not affected by the action of AFunction, which has a local variable of the same name; the function MessageBox displays the string “YES,” which was the value assigned to the global variable szVal.

    STRING szVal;

    

    prototype AFunction();

    

    . . .

    

    szVal = "YES";

    AFunction();

    MessageBox(szVal, INFORMATION);

    

    . . .

    

function AFunction()

  STRING szVal;

begin

  szVal = "NO";

end;

Parameter names in function definitions are considered to be local variables. If a global variable is passed to a function whose parameter has the same name as the global variable, the value of that global variable will not be changed (unless the parameter was specified with the BYREF operator in the function prototype). In the following example, AFunction has no effect on the global variable szVal; the script displays the string “YES”.

    STRING szVal;

    prototype AFunction(STRING);

    

    . . .

    

    szVal = "YES";

    AFunction(szVal);

    MessageBox(szVal, INFORMATION);

    

    

    . . .

    

function AFunction(szVal)

begin

  szVal = "NO";

end;

See Also