Choose
Selects and returns a value from a list of arguments by 1-based index.
Syntax: Choose( index , choice-1 [ , choice-2 , … [ , choice-n ] ] )
- index
- required Numeric expression that evaluates to a value between 1 and the number of available choices.
- choice
- required Variant expression containing one of the possible choices.
If index is 1, Choose returns choice-1; if index is 2, it returns choice-2; and so on. If index is less than 1 or greater than the number of choices listed, Choose returns Null. Non-integer values of index are rounded to the nearest whole number before being evaluated.
Note
An index outside the range of a Single raises error 6, Overflow, in twinBASIC, as in 64-bit VBA. VB6 returns Null for it.
Note
Choose evaluates every choice in the list, not only the one it returns. Watch for side effects: a MsgBox call inside any of the choices is invoked once per choice, not just for the selected one. To avoid this — for example, when one of the branches would error out — use the short-circuiting If function instead.
Common Errors
| Error | When |
|---|---|
94 (&H5E), Invalid use of Null | index is Null |
6 (&H6), Overflow | index is outside the range of a Single |
Example
This example uses Choose to map a 1-based option index to a name.
Function GetChoice(Ind As Integer) As String
GetChoice = Choose(Ind, "Speedy", "United", "Federal")
End Function