Professional Documents
Culture Documents
Variable Scope Crystal Reports
Variable Scope Crystal Reports
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.
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.