<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - choose - Logic">
<title>choose - Logic - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/logic/index.html">Logic</a> / choose</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="choose-function">Choose Function</h1>
<p>Returns a value from a list of choices based on an index.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Choose(index, choice-1[, choice-2, ... [, choice-n]])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>index</strong>: Required. Numeric expression (typically Integer or Long) that results in a value
between 1 and the number of available choices. The index is 1-based.</li>
<li><strong>choice</strong>: Required. Variant expression containing one of the possible choices. You must
provide at least one choice argument.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>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, <code>Choose</code> returns <code>Null</code>.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Choose</code> 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:
- Mapping numeric codes to string values
- Selecting values based on calculated indices
- Simplifying multi-way conditional expressions
- Implementing lookup tables inline
<strong>Important Characteristics:</strong>
- The index is 1-based (first choice is at index 1)
- Returns <code>Null</code> if index is out of range (< 1 or > number of choices)
- All choice arguments are evaluated before selection occurs (unlike <code>IIf</code>)
- Can accept any Variant-compatible expressions as choices
- Choices can be of mixed types (numbers, strings, objects, etc.)
- The returned value's type depends on the selected choice</p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="mapping-codes-to-descriptions">Mapping Codes to Descriptions</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="dynamic-message-selection">Dynamic Message Selection</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="month-name-lookup">Month Name Lookup</h3>
<pre><code class="language-vbnet">Function GetMonthName(monthNum As Integer) As String
GetMonthName = Choose(monthNum, _
"January", "February", "March", "April", _
"May", "June", "July", "August", _
"September", "October", "November", "December")
End Function</code></pre>
<h3 id="grade-calculation">Grade Calculation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="configuration-selection">Configuration Selection</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="color-code-mapping">Color Code Mapping</h3>
<pre><code class="language-vbnet">Function GetColorValue(colorCode As Integer) As Long
GetColorValue = Choose(colorCode, _
vbRed, vbGreen, vbBlue, vbYellow, _
vbMagenta, vbCyan, vbWhite, vbBlack)
End Function</code></pre>
<h3 id="day-of-week-operations">Day of Week Operations</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="file-extension-mapping">File Extension Mapping</h3>
<pre><code class="language-vbnet">Function GetFileTypeDescription(fileType As Integer) As String
GetFileTypeDescription = Choose(fileType, _
"Text Document", _
"Spreadsheet", _
"Database", _
"Image File", _
"Executable Program")
End Function</code></pre>
<h3 id="priority-level-mapping">Priority Level Mapping</h3>
<pre><code class="language-vbnet">Function GetPriorityWeight(priority As Integer) As Double
GetPriorityWeight = Choose(priority, 0.25, 0.5, 1.0, 2.0, 5.0)
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="with-null-handling">With Null Handling</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="combined-with-calculation">Combined with Calculation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="nested-choose">Nested Choose</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 450</strong> (Wrong number of arguments): Occurs if you don't provide at least one choice</li>
<li><strong>Error 13</strong> (Type mismatch): Can occur if index expression is not numeric</li>
<li><strong>Null return</strong>: Index is < 1 or > number of choices (not an error, but important to handle)</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>All choice arguments are evaluated before selection, even if not used</li>
<li>For expensive computations, consider using <code>Select Case</code> instead</li>
<li>Choose is most efficient with literal values or simple expressions</li>
<li>For large lookup tables, consider using arrays or collections</li>
</ul>
<h2 id="comparison-with-alternatives">Comparison with Alternatives</h2>
<h3 id="choose-vs-select-case">Choose vs. Select Case</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<p><strong>Choose advantages:</strong>
- More concise for simple selections
- Can be used as an expression
- Good for inline value lookup
<strong>Select Case advantages:</strong>
- Doesn't evaluate all branches
- Better for complex logic
- More readable for many cases
- Supports range matching</p>
<h3 id="choose-vs-array-lookup">Choose vs. Array Lookup</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>All arguments are evaluated even if not selected (performance impact)</li>
<li>Limited to 29 arguments in VB6 (index + 28 choices max in practice)</li>
<li>Index must be numeric (Integer, Long, Byte, etc.)</li>
<li>Returns Null for out-of-range indices (requires explicit checking)</li>
<li>Cannot use named arguments</li>
<li>Not suitable for ranges or complex matching logic</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Switch</code>: Similar but uses condition/value pairs instead of index-based selection</li>
<li><code>IIf</code>: Binary choice based on condition (evaluates both branches)</li>
<li><code>Select Case</code>: Statement-based multi-way branching</li>
<li><code>Array</code>: Creates an array that can be indexed (0-based)</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Logic</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>