Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

7.12 Switch()

The Switch() Function

The Switch() function is a slight variation of the Choose() function. The Switch() function uses pairs of test conditions and return values. The first parameter is an expression to test and the second parameter is a result value that is returned if the expression is true. This is repeated with every two parameters being in one group.

The syntax for the Switch() function is as follows:

Var := Switch(condition1, result1, condition2, result2, ….);

This function can return any data type except for an array. The data types of each result must be the same.

What makes this unique from the If Then and Select statements is that every parameter is evaluated before a result is returned from the function. Even if the first parameter is true, all the remaining parameters are still evaluated. This can have good or bad consequences depending upon your needs. The result can be bad because there could be a performance issue if you are passing it time-intensive functions. It could also result in an error being raised if every condition wasn’t meant to be evaluated (e.g. divide by zero errors).

Executing every line can be good if you want to force various functions to be called prior to returning a value. For example, it can perform a list of financial functions prior to returning a value.