### Variable Scope

Variable scopes are used to define the degree to which variables in one formula are made available to other formulas. There are three levels of scope in Seagate Crystal Reports: local, global and shared. Every variable has a scope, and this scope is specified when the variable is declared.

#### Local Variables

Variables with local scope, also known as local variables, are declared using the Local keyword followed by the type name (with the Var suffix) followed by the variable name as in the above examples.

Local variables are restricted to a single formula and a single evaluation of that formula. This means that you cannot access the value of a local variable in one formula from a different formula.

##### Example

    //Formula A
    Local NumberVar x;
    x := 10;
    //Formula B
    EvaluateAfter ({@Formula A})
    Local NumberVar x;
    x := x + 1;

The function call EvaluateAfter ({@Formula A}) ensures that Formula B will be evaluated after Formula A is evaluated. Formula A returns a value of 10 and Formula B returns a value of 1. Formula B does not have access to Formula A's x and thus cannot use the value of 10 and add 1; instead, it uses the default value for the uninitialized local variable x found in Formula B, which is 0, and adds 1 to it to get 1.

You can also create local variables with the same name but different types in different formulas. For example, the type declarations in formulas A and B do not conflict with:

    //Formula C
    Local StringVar x := "hello";

Local variables are the most efficient of the three scopes. They also do not interfere with one another in different formulas. For these reasons, it is best to declare variables to be local whenever possible.

Seagate Software, Inc. http://www.seagatesoftware.com Please send comments to: techpubs@seagatesoftware.com