Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Variable Scope (Crystal Syntax)

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 Crystal Reports:
Local
Global
Shared
Every variable has a scope, and this scope is specified when the variable is declared.
Local Variables (Crystal Syntax)
Variables with local scope are declared using the Local keyword followed by the type name
(with the Var suffix) followed by the variable name.
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. In addition, they do not interfere
with one another in different formulas. For these reasons, it is best to declare variables to be
local whenever possible.

Global Variables (Crystal Syntax)


Global variables use the same memory block to store a value throughout the main report. This
value is then available to all formulas that declare the variable, except for those in subreports.
You declare a global variable as in the following example:
Global StringVar y;
You can also omit the Global keyword which creates a Global variable by default:
StringVar y; //Same as: Global StringVar y;
However, even though global variables are easy to declare, it is recommended that you use
them only when local variables do not suffice.
Since global variables share their values throughout the main report, you cannot declare a
global variable in one formula with one type and then declare a global variable with the same
name in a different formula with a different type.
Example
//Formula A
Global DateVar z;
z := CDate (1999, 9, 18)
//Formula B
NumberVar z;
z := 20
In this case, if you enter and save Formula A first, Crystal Reports will return an error when
you check or try to save Formula B. This is because the declaration of the Global variable z
as a Number conflicts with its earlier declaration in Formula A as a Date.
When to Use Global Variables
Global variables are often used to perform complex calculations where the results of a
formula depend upon the grouping and page layout of the actual printed report. This is
accomplished by creating several formulas, placing them in different sections of the report,
and having the different formulas interact via global variables.
Example
//Formula C
Global NumberVar x;
x := 10;
//Formula D
//Call the function WhileReadingRecords
WhileReadingRecords;
Global NumberVar x;
x := x + 1

If Formula C is placed in the Report Header and then Formula D is placed in a detail section,
Formula C will be evaluated before Formula D. Formula C will be evaluated once and then
Formula D will be evaluated for each record appearing in the detail section. Formula C
returns 10. For the first detail record, Formula D returns 11. This is because the value 10 of x
is retained from when it was set by Formula C. Formula D then adds 1 to this value, setting x
to 11 and then returns 11. For the second detail record, formula D return 12, adding 1 to the
previously retained value of x which was 11. This process continues for the remaining detail
records.
The call to WhileReadingRecords tells Crystal Reports to re-evaluate Formula D as it reads in
each record of the report. Otherwise, since the formula does not contain any database fields,
the program evaluates it only once before reading the records from the database. The formula
will then return the value 11 instead of 11, 12, 13, ... as the successive records are processed.
If the expression x := x + 1 is replaced by x := x + {Orders Detail.Quantity}, you create the
effect of a running total based on {Orders Detail.Quantity}, although it is one starting at 10
rather than 0 because of Formula C. In this case, you can omit the call to
WhileReadingRecords, since it will automatically occur because the formula contains a
database field.
Shared Variables (Crystal Syntax)
Shared variables use the same memory block to store the value of a variable throughout the
main report and all of its subreports. Thus shared variables are even more general than global
variables. To use a shared variable, declare it in a formula in the main report as in the
following example:
Shared NumberVar x := 1000;
and declare it in a formula in the subreport as in the following example:
Shared NumberVar x;
In order to use shared variables, the variable must be declared and assigned a value before it
can be passed between the main report and the subreport.

You might also like