Declaring Variables
Variables store a piece of data so that it can be used again within the formula. For example, if a calculation is performed multiple times in a formula, then you can store the result in a variable and reference that variable wherever it’s needed. This makes the formula easier to read and it improves performance because you don’t have to repeat the calculation. Variables are also used to pass data between other formulas and even to sub-reports. For example, by declaring a variable as Global it can be used by other formulas. If you couldn’t share variables, then you would have to duplicate the same calculation in different report sections.
When a variable is declared, it is automatically assigned a default value. For example, a NumberVar variable is assigned the value 0. See Table 7-1 in the previous section for a list of the default values for each data type.
To use a variable in a formula, you first have to declare it. Declaring a variable follows the format of specifying the scope followed by the data type and the variable name.
A variable’s scope determines which formulas have access to that variable. You can set the scope so that a variable can only be used within the formula it’s declared in, or you can make it available to the rest of the report. There are three operators that are used to declare scope:
- Local (Dim for Basic syntax): The variable can only be seen within the current formula. The variable is private to that formula and can’t be used anywhere else in the report. This is the default scope if you don’t specify it. Dim is the default scope for Basic syntax.
- Global: The variable can be seen within any formula inside the same report. Sub-reports do not have access to the variable. Global is the default scope for Crystal syntax.
- Shared: Similar to Global, but the variable can also be seen within sub-reports.
If you want to specify the variable’s default value, you can do so in the variable declaration statement. For example, the next example declares the variable PI as a global number and assigns a value of 3.14 to it. Now any formula in the report (or sub-report) can reference it.
Local variables and global variables also differ in how they get initialized. Local variables are always reset back to their default value every time the formula is called. Thus, the result of any calculations made to a variable are cleared when the formula exits and it doesn’t have any affect the next time the formula is executed. But global variables keep their value the entire time the report is running. Once you declare a variable and assign a value to it, that variable retains its value for the life of the report. Any future calls to that same formula will not initialize the global variable back to its default value. If you need to have a global variable reset back to its default value every time the formula is called, you need to write this initialization code at the beginning of the formula.
Crystal Reports doesn’t reset global variables because of two reasons. The first is that a global variable is designed to be shared with other functions. If Crystal Reports reset it when exiting the formula, then the other formulas that reference it would only see the default value (zero, an empty string, etc.). This defeats the purpose of sharing variables. Another reason is the way formulas and report sections work together. Formulas are usually added to a report section that is printed multiple times. If a global variable lost its value every time a new record was processed, then you wouldn’t be able to track what happened when the last record was processed. Making it easy for variables to retain their values while different records are processed is one reason why Crystal Reports is so powerful.
All variables must be declared in the formula that uses them. This might seem obvious because we already learned that if you don’t declare a variable then Crystal Reports won’t know its scope or what data type it’s supposed to be. But if a variable has a scope of Global or Shared, then you might think that another formula has already declared it so there is no need to repeat this information. Any formula should be able to reference it anywhere in the report. But this isn’t the case. You still have to declare variables in each formula that uses it. For example, if you declare a variable as Global, Crystal Reports will search the other formulas for another global variable with the same name. If it finds one, then it will have them share the same value.
If you don’t re-declare the variable, Crystal Reports gives an error stating that the variable doesn’t exist.