PRB: All Parameters of Choose Function Are EvaluatedLast reviewed: November 3, 1997Article ID: Q175979 |
The information in this article applies to:
SYMPTOMSWhen you specify functions as the Choice arguments in a Choose function, all functions are executed.
CAUSEThe 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 RESOLUTIONBecause 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 IfEquivalent 'Select Case' Statement
Select Case MyIndex Case 1 result = MyFunction1() Case 2 result = MyFunction2() Case 3 result = MyFunction3() Case Else result = Null End Select STATUSThis behavior is by design.
MORE INFORMATION
Steps to Reproduce Behavior
REFERENCESVisual Basic's Online Help for the Choose function. Keywords : VB4WIN vb5all Version : WINDOWS:4.0,5.0 Platform : WINDOWS Issue type : kbprb |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |