CAUSE
The Choose function allows you to select a value from a list of choices
based on the value passed as the index. For example, the following
statement would result in x being assigned the value "b":
x = Choose(2, "a", "b", "c")
The parameters passed to the Choose function do not need to be static
values as in the above example. You can use function names instead:
x = Choose(2, MyFunction1(), MyFunction2(), MyFunction3())
In this situation, all of the functions referenced are executed regardless
of the index specified. In this example, the functions MyFunction1,
MyFunction2, and MyFunction3 are all executed even though the result of
MyFunction2 is the value returned by Choose.
Choose is implemented as a function with a parameter array and every
parameter must be evaluated before it can be passed to the Choose function.
Visual Basic's implementation of the Choose function can be duplicated with
the following function:
Public Function MyChoose(Index As Single, ParamArray Choice())
On Error GoTo BadChoice
If IsObject(Choice(Index - 1)) Then
Set MyChoose = Choice(Index - 1)
Else
MyChoose = Choice(Index - 1)
End If
Exit Function
BadChoice:
MyChoose = Null
End Function
RESOLUTION
Because this behavior is by design, another approach must be taken to avoid
all parameters being evaluated. The logic of the Choose function can easily
be broken down into an equivalent "If...Then...ElseIf" statement or a
"Select Case" statement.
Sample Choose Statement
result = Choose(MyIndex, MyFunction1(), MyFunction2(), MyFunction3())
Equivalent 'If...Then...ElseIf' Statement
If MyIndex = 1 Then
result = MyFunction1()
ElseIf MyIndex = 2 Then
result = MyFunction2()
ElseIf MyIndex = 3 Then
result = MyFunction3()
Else
result = Null
End If
Equivalent 'Select Case' Statement
Select Case MyIndex
Case 1
result = MyFunction1()
Case 2
result = MyFunction2()
Case 3
result = MyFunction3()
Case Else
result = Null
End Select