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

5.string (’Hello’)

6.time (11:59:01)

7.dateTime (96/12/31 11:59:59 P.M.)

How to declare a variable

You must declare a variable at the beginning of the formula that uses the variable.

NOTE: You can not declare variables globally. If you are using a variable that was declared in another formula, you must declare it again.

NOTE: If you declare a variable with the same name and data type in two or more formulas, the formulas share the same variable. Thus, if one formula sets the value of the variable, the variable in the second (and additional formulas) reflects the change.

To declare a variable you must enter:

the datatype and the variable name,

followed by a semicolon to mark the end of the declaration.

For example, to declare a number variable named Amount, enter the following declaration statement:

NumberVar Amount;

If you want to declare a Boolean variable named Outstanding, enter the following declaration statement:

BooleanVar Outstanding;

If you want to declare more than one variable, you can list them.

Each variable is separated by a semicolon. For example:

NumberVar Amount;

BooleanVar Outstanding;

DateVar MonthEnd;

The program uses your declaration statement to set aside a block of memory that holds each of the variable values and assigns a default value to each memory block. The default value assigned

Advanced Formulas

353

depends on the data type you declare for the variable. The default values assigned are as follows:

Data type

To

Default value

 

 

 

 

 

 

number

NumberVar

0

 

 

 

currency

CurrencyVar

0

 

 

 

Boolean

BooleanVar

False (No, 0)

 

 

 

date

DateVar

date (0, 0, 0)

 

 

 

string

StringVar

empty string (“”)

 

 

 

dateTime

DateTimeVar

No default1

time

TimeVar

No default1

1 Since Time (00:00:00) is midnight, you can not use 00:00:00 as a default time and you can not use this time value as part of a default dateTime either. Thus, a default is not assigned here.

How to assign a value to a variable

Variable name

Assignment operator

Variable value

You assign a value to a variable using an assignment statement. The assignment statement consists of:

the variable name,

the assignment operator, and

the value you want to assign to the variable.

The variable name is the name used to declare the variable.

The assignment operator is a colon followed by an equals sign (:=). Search for Assignment operator in Seagate Crystal Reports online Help.

The variable value is any value that matches the data type of the variable. For example, you can assign a number to a number variable. You can also assign an expression that results in a number or even a sequence of expressions. For a string variable you could assign a character, a word, a sentence, or an expression that results in a string. A variable value can be a constant, an expression, or a sequence of expressions.

354

Seagate Crystal Reports User’s Guide

EXAMPLE ASSIGNMENT STATEMENT

Following are the assignment statements for assigning different kinds of values to variables:

Amount:= 0

«Assigns (initializes) the variable Amount to zero.»

Amount:= 100

«Assigns the value 100 to the variable Amount.»

Amount:= Amount + {orders detail.QUANTITY}

«Assigns the result of a calculation to the variable Amount. The calculation adds the value of the quantity field {orders detail.QUANTITY} to the current value of the Amount variable. This type of expression is useful in running total situations where each running total consists of the current amount plus an additional value. See Advanced Totalling, Page 377

Amount:= {file.QUANTITY1} + {file.QUANTITY2} + {file.QUANTITY3}

«Adds the three quantity fields and assigns the result to the variable Amount.»

Customer:= “Westside Motors”

«Assigns the string “Westside Motors” to the variable Customer.»

Customer:= {customer.FIRST NAME} + {customer.LAST NAME}

«Adds (concatenates) two string fields and assigns the concatenated value to the variable Customer.»

Customer:= TrimRight({customer.FIRST NAME}) + {customer.LAST NAME}

«Trims the trailing blanks from the First Name field ({customer.FIRST NAME}), concatenates that field to the Last Name field ({customer.LAST NAME}), and assigns the concatenated value to the variable Customer.»

Advanced Formulas

355

Customer:= “Mr. “ + {customer.LAST NAME}

«Concatenates the string “Mr. “ with the value of the Last Name field {customer.LAST NAME}, and assigns the concatenated value to the variable Customer.»

Amount:= 100;

Customer:= “Westside Motors”;

«Assigns the constant 100 to the number variable named Amount, and assigns the string “Westside Motors” to the variable Customer. You can assign values to multiple variables by separating the assignment statements with semicolons.»

How to combine a variable declaration and assignment expression

For efficiency, you can declare a variable and assign it a value in a single line of formula code. To do this, simply declare the variable, allow a blank space, enter the assignment operator, and assign the value.

For example, to declare a currency variable SellPrice and assign the value of the Cost field ({product.PRICE (SRP)}) times two (a 100% markup), use the following expression:

CurrencyVar SellPrice:={product.PRICE(SRP)} * 2;

To declare a Boolean variable OverQuota and assign the result of the comparison {file.SALES}>{file.QUOTA}, use the following expression:

BooleanVar OverQuota:= {file.SALES}>{file.QUOTA};

356

Seagate Crystal Reports User’s Guide

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