VB6Parse / Library / Logic / choose

VB6 Library Reference

Choose Function

Returns a value from a list of choices based on an index.

Syntax

Choose(index, choice-1[, choice-2, ... [, choice-n]])

Parameters

Return Value

Returns a Variant containing the value of the choice at the specified index position. If the index is less than 1 or greater than the number of choices, Choose returns Null.

Remarks

The Choose function provides a convenient way to select from a list of values based on an index, similar to a switch/case statement but as an expression. It's particularly useful for:

Important Characteristics:

Examples

Basic Usage

' Simple value selection
Dim dayType As String
dayType = Choose(Weekday(Date), "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

' Numeric selection
Dim priority As Integer
priority = Choose(level, 1, 5, 10, 50, 100)

' Mixed types
Dim result As Variant
result = Choose(2, 100, "Text", #1/1/2000#, True)  ' Returns "Text"

Mapping Codes to Descriptions

Function GetStatusDescription(statusCode As Integer) As String
GetStatusDescription = Choose(statusCode, _
"Pending", _
"Approved", _
"Rejected", _
"On Hold", _
"Completed")

If IsNull(GetStatusDescription) Then
GetStatusDescription = "Unknown"
End If
End Function

Dynamic Message Selection

Sub ShowErrorMessage(errorLevel As Integer)
Dim msg As String
msg = Choose(errorLevel, _
"Operation completed successfully.", _
"Warning: Please review the results.", _
"Error: Operation failed.", _
"Critical: System error occurred.")

MsgBox msg, vbInformation
End Sub

Common Patterns

Month Name Lookup

Function GetMonthName(monthNum As Integer) As String
GetMonthName = Choose(monthNum, _
"January", "February", "March", "April", _
"May", "June", "July", "August", _
"September", "October", "November", "December")
End Function

Grade Calculation

Function GetLetterGrade(score As Integer) As String
Dim gradeIndex As Integer
gradeIndex = Int(score / 10) - 5  ' 60-69=1, 70-79=2, etc.
If gradeIndex < 1 Then gradeIndex = 1
If gradeIndex > 5 Then gradeIndex = 5

GetLetterGrade = Choose(gradeIndex, "F", "D", "C", "B", "A")
End Function

Configuration Selection

Function GetServerURL(environment As Integer) As String
' 1=Development, 2=Testing, 3=Staging, 4=Production
GetServerURL = Choose(environment, _
"http://localhost:8080", _
"http://test.example.com", _
"http://staging.example.com", _
"https://www.example.com")
End Function

Color Code Mapping

Function GetColorValue(colorCode As Integer) As Long
GetColorValue = Choose(colorCode, _
vbRed, vbGreen, vbBlue, vbYellow, _
vbMagenta, vbCyan, vbWhite, vbBlack)
End Function

Day of Week Operations

Function IsBusinessDay(dayOfWeek As Integer) As Boolean
' Sunday=1, Monday=2, ... Saturday=7
IsBusinessDay = Choose(dayOfWeek, _
False, True, True, True, True, True, False)
End Function

File Extension Mapping

Function GetFileTypeDescription(fileType As Integer) As String
GetFileTypeDescription = Choose(fileType, _
"Text Document", _
"Spreadsheet", _
"Database", _
"Image File", _
"Executable Program")
End Function

Priority Level Mapping

Function GetPriorityWeight(priority As Integer) As Double
GetPriorityWeight = Choose(priority, 0.25, 0.5, 1.0, 2.0, 5.0)
End Function

Advanced Usage

With Null Handling

Function SafeChoose(index As Integer, ParamArray choices()) As Variant
Dim result As Variant
Dim i As Integer
Dim args As String

' Build argument list
For i = LBound(choices) To UBound(choices)
If i > LBound(choices) Then args = args & ", "
args = args & """" & choices(i) & """"
Next i

' Use Execute to call Choose dynamically
result = Choose(index, choices(0), choices(1), choices(2))  ' etc.

If IsNull(result) Then
SafeChoose = "Invalid Index"
Else
SafeChoose = result
End If
End Function

Combined with Calculation

Function CalculateDiscount(customerType As Integer, amount As Currency) As Currency
Dim discountRate As Double
discountRate = Choose(customerType, 0.05, 0.1, 0.15, 0.2)

If IsNull(discountRate) Then
discountRate = 0
End If

CalculateDiscount = amount * discountRate
End Function

Nested Choose

Function GetRegionalGreeting(region As Integer, timeOfDay As Integer) As String
' region: 1=North, 2=South, 3=East, 4=West
' timeOfDay: 1=Morning, 2=Afternoon, 3=Evening

GetRegionalGreeting = Choose(region, _
Choose(timeOfDay, "Good morning, y'all", "Good afternoon, y'all", "Good evening, y'all"), _
Choose(timeOfDay, "Mornin'", "Afternoon", "Evenin'"), _
Choose(timeOfDay, "Good morning", "Good afternoon", "Good evening"), _
Choose(timeOfDay, "Hey, morning!", "Hey, afternoon!", "Hey, evening!"))
End Function

Error Handling

Function SafeChooseWithError(index As Integer) As Variant
On Error GoTo ErrorHandler

Dim result As Variant
result = Choose(index, "First", "Second", "Third")

If IsNull(result) Then
MsgBox "Index out of range: " & index
SafeChooseWithError = Empty
Else
SafeChooseWithError = result
End If

Exit Function

ErrorHandler:
MsgBox "Error in Choose: " & Err.Description
SafeChooseWithError = Empty
End Function

Common Errors

Performance Considerations

Comparison with Alternatives

Choose vs. Select Case

' Using Choose (expression-based)
result = Choose(index, "A", "B", "C")

' Using Select Case (statement-based)
Select Case index
Case 1: result = "A"
Case 2: result = "B"
Case 3: result = "C"
Case Else: result = Null
End Select

Choose advantages: - More concise for simple selections - Can be used as an expression - Good for inline value lookup

Select Case advantages: - Doesn't evaluate all branches - Better for complex logic - More readable for many cases - Supports range matching

Choose vs. Array Lookup

' Using Choose
result = Choose(index, "A", "B", "C")

' Using Array
Dim choices() As Variant
choices = Array("A", "B", "C")
If index >= 1 And index <= UBound(choices) + 1 Then
result = choices(index - 1)  ' Array is 0-based, Choose is 1-based
Else
result = Null
End If

Limitations

← Back to Logic | View all functions