Article ID: 175979
Article Last Modified on 7/16/2004
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.
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
result = Choose(MyIndex, MyFunction1(), MyFunction2(), MyFunction3())
If MyIndex = 1 Then
result = MyFunction1()
ElseIf MyIndex = 2 Then
result = MyFunction2()
ElseIf MyIndex = 3 Then
result = MyFunction3()
Else
result = Null
End If
Select Case MyIndex
Case 1
result = MyFunction1()
Case 2
result = MyFunction2()
Case 3
result = MyFunction3()
Case Else
result = Null
End Select
Option Explicit
Private Function MyFunction1() As Long
Debug.Print "MyFunction1"
MyFunction1 = 1
End Function
Private Function MyFunction2() As Long
Debug.Print "MyFunction2"
MyFunction2 = 2
End Function
Private Function MyFunction3() As Long
Debug.Print "MyFunction3"
MyFunction3 = 3
End Function
Private Sub Command1_Click()
Dim result
result = Choose(2, MyFunction1(), MyFunction2(), MyFunction3())
End Sub
Additional query words: kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbVBp400 kbVBA
Keywords: kbprb KB175979