Description
Choose returns a value from the list of choices based on the value of index.
Arguments
index is a Number or numeric expression that specifies the index of the choice. It should be between 1 and the number of available choices. If it is out of bounds, Choose returns a default value. (The default value returned depends on the type of the choices. For example, if the choices are of Number type, the default value is 0 and if the choices are of String type, the default value is the empty string ("").)
choice is one of the choices to choose from. All choices must be of the same type. A choice can be any simple type (Number, Currency, String, Boolean, Date, Time or DateTime) or range type (Number Range, Currency Range, String Range, Date Range, Time Range or DateTime Range), but it may not be an array.
Returns
A value from the given list of choices. The type of the returned value is the same as the type of the choices.
Action
Choose returns a value from the list of choices based on the value of index. For example, if index is 1, it returns choice1 and if index is 2 it returns choice2 and so forth.
Typical uses
Choose can be used instead of the If-Then-Else operators or the Select control structures in some situations. For example, if you are mapping an index value to a String value, it can be simplest to use Choose.
One situation where Choose may be better than a control structure is when writing record selection formulas so that they can be pushed down to the database server.
See "Using enhanced record selection formulas" in the Designing Optimized Web Reports chapter of the Crystal Reports User's Guide for an explanation of the techniques involved.
Examples
Choose (3, "Poor", "Fair", "Good", "Excellent")
Returns the String "Good".
Choose (2, 10 To 20, 100 To 200, 60 To 70)
Returns the Number Range value 100 To 200.
If a company has fiscal year starting July 1 (the 3rd calendar quarter) and it wants to print the fiscal quarter of each order date:
Choose (DatePart ("q", {Orders.Order Date}),
"third fiscal quarter", "fourth fiscal quarter",
"first fiscal quarter", "second fiscal quarter")
If {Orders.Order Date} is February 18, 1996, then the DatePart function call returns 1 since this date is in the first calendar quarter. Thus, the first choice value, namely the String value "third fiscal quarter" is returned by the formula.
Comments
Every argument of the Choose function is evaluated before the choice is returned. Thus, you should watch out for undesirable side effects when using Choose. For example, if one of the choice expressions results in division by zero, an error will occur, even if that choice is not the choice that is returned by the Choose function.