Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

8.10 Choose() and Switch()

The Choose() Function

The Choose() function returns a value chosen from a list of values. The value returned is determined by an index that is passed to the function. This function is like a shortcut for the If statement and the Select Case statement. You can use it when the range of possible values is relatively small and sequential.

The first parameter is an index representing which item to return. The remaining parameters are the items to choose from. The index range starts at 1 (it’s not zero based). If the index is a value that is greater than the number of items passed, the default value for the appropriate data type is returned (e.g. zero for numbers, “” for strings). This function can return any data type except for an array. As expected, each item in the list must be of the same data type.

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

Var = Choose(index, value1, value2, value3, ...)

The Switch() Function

The Switch() function is also like a shortcut for the If statement and the Select Case statement. The parameters are grouped in pairs. The first parameter is an expression to test and the second parameter is a result value that is returned if the expression is true.

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

Var = Switch(condition1, result1, condition2, result2, ....)

What makes this unique from the If and Select Case statements is that every parameter is evaluated before a result is returned from the function. This can have good or bad results depending upon your needs. The result can be bad because there could be a performance issue if you are passing time-intensive functions as parameters or it could result in an error being raised. It can be good if you want to force various functions to be called prior to returning a value. This function can return any data type except for an array. The data types of each result must be the same.