Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CRW_REF.PDF
Скачиваний:
5
Добавлен:
13.02.2015
Размер:
13.99 Mб
Скачать

How to declare and assign values to multiple variables

There may be times that you want to declare multiple variables and assign values to each of them. To do this in the most efficient manner, simply chain the declaration/assignment expressions together, separating them with semicolons.

For example, to declare two variables (a number variable Quantity, and a currency variable SellPrice) and then assign values to each variable (the number 5 to the variable Quantity, and {file.COST} * 2 to the variable SellPrice), use expressions similar to the following:

NumberVar Quantity:= 5;

CurrencyVar SellPrice:= {file.COST} * 2;

How to conditionally assign values to variables

Seagate Crystal Reports formula language gives you the ability to assign different values to variables based on conditions being met or not met. Consider the following formula:

NumberVar Total;

NumberVar Result;

Total:= Total + {invoices.ITEM TOTAL};

If Next ({invoices.CUST#})<>{invoices.CUST#}

Then

(Result:= Total; Total:= 0)

Else

Result:= 0;

Result;

The If-Then-Else part of this formula says that when the If condition is met (if the customer numbers [{invoices.CUST#}] are not equal), the program is to do two separate things:

1.Assign the value stored in the variable Total (the running total) to the variable Result.

2.Reset the value in the variable Total to 0.

Advanced Formulas

357

When the If condition is not met (if the customer numbers are equal), the program is to assign the value 0 to the variable Result.

How to use an array in a formula

An array is a special type of variable that can hold several values at once. The entire array can be passed to a summary function for evaluation, or separate elements of the array can be extracted using the Subscript operator. Search for Subscript in Seagate Crystal Reports online Help.

A common use for an array is to store the names of the days of the week:

StringVar array Weekdays:= [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”];

There are five parts to declaring an array variable:

1.A variable declaration operator specific to the type of data the array will hold, StringVar in this example.

2.The word array follows the variable declaration operator.

3.The name you assign to the array follows the word array, Weekdays in this example.

4.The Assignment operator follows the name and is used to assign values to the array variable.

5.Square brackets follow the assignment operator and are used to enclose the values (elements) stored by the array. Each element is separated by a comma.

Search for Assignment in Seagate Crystal Reports online Help.

An index value is assigned to each element in the array according to the order of the elements.

The first element is assigned index 1,

The second element is assigned index 2, and so on.

358

Seagate Crystal Reports User’s Guide

To extract an element from the following array in your code:

StringVar array Weekdays:= [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”];

use the Subscript operator with the index number for the element you want to extract:

Weekdays[5]

«Returns “Thursday”.»

Negative numbers can also be used to extract array elements:

Weekdays[-4]

«Returns “Wednesday”.»

You can create an array of values for any valid data type. However, arrays have the following restrictions:

All elements must be of the same data type.

You must declare the array with one of the variable declaration operators.

There can be a maximum of 100 elements in an array.

Each element in an array of string values can have a maximum of 254 characters (the standard limit of any string value in Seagate Crystal Reports).

Examine the following examples of array declarations to become more familiar with arrays:

NumberVar array x := [1, 10, 44];

CurrencyVar array Cost := [$19.95, $79.50, $110.00, $44.79, $223.99];

DateVar array PayDays := [Date(1999, 05, 15), Date(1999, 05, 31)];

Advanced Formulas

359

Using arrays

with summary functions

A formula example

Arrays can also be used without being assigned to array variables. For example:

[“One”, “Two”, “Three”][2]

«Returns “Two” because it is the second item in the array.»

In some situations, you may prefer to use arrays dynamically like this. However, most formula situations that require arrays can be handled more easily by defining the array as an array variable.

Summary functions accept arrays as parameters without requiring the array be declared as a variable. For example:

Average([5, 10, 15]) = 10

Use the square brackets to indicate that an array is being used with the function. Search for Summary Functions in Seagate Crystal Reports online Help.

To better understand how arrays can be used in formulas, examine the following formula example:

StringVar array Weekdays := [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”];

Weekdays[DayOfWeek({orders.SHIP DATE})]

«If DayOfWeek is 2, Monday will be returned because it is the second item in the array.»

This formula prints the name of the day of the week that each order was shipped. First, the Weekdays array is declared and assigned string values for each day of the week. Search for DayOfWeek in Seagate Crystal Reports online Help.

Next, the DayOfWeek function evaluates the date stored in the {orders.SHIP DATE} field and returns a number representing the day of the week (1 for Sunday, 2 for Monday, etc.).

Finally, the Subscript operator is used with the Weekdays array to retrieve the name of the day of the week according to the number returned by the DayOfWeek function. The name of the day is returned by the formula and appears in the report. Search for Subscript in Seagate Crystal Reports online Help.

360

Seagate Crystal Reports User’s Guide

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]