VB6 Switch Function
The Switch function evaluates a list of expressions and returns a value or expression associated with the first expression that is True.
Syntax
Switch(expr1, value1[, expr2, value2 ... [, expr_n, value_n]])
Parameters
expr1, expr2, ..., expr_n: Required. Variant expressions to be evaluated.value1, value2, ..., value_n: Required. Values or expressions to be returned. If the associatedexpris True,Switchreturns the corresponding value.
The argument list consists of pairs of expressions and values. The expressions are evaluated from left to right, and the value associated with the first expression to evaluate to True is returned. If the pairs aren't properly matched, a run-time error occurs.
Returns
Returns a Variant containing the value associated with the first True expression. If no expression is True, Switch returns Null.
Remarks
The Switch function provides a flexible way to select from multiple alternatives:
- Left-to-right evaluation: Expressions are evaluated in order until one is True
- Short-circuit evaluation: Once a True expression is found, remaining expressions are not evaluated
- Null return: If no expression evaluates to True, returns Null
- Pairs required: Arguments must come in pairs (expression, value). Odd number of arguments causes Error 450
- Value can be expression: The value part can be a literal, variable, or expression
- All values evaluated: Unlike expressions, all values in the argument list may be evaluated (implementation-dependent)
- Type flexibility: Can return different types for different cases (returns Variant)
- Compare with Select Case: Switch is an expression (returns value), Select Case is a statement
- Compare with
IIf: Switch handles multiple conditions,IIfhandles only two branches
Evaluation Behavior
- Expressions are evaluated left to right
- First True expression stops evaluation of remaining expressions
- Values may be evaluated even if their associated expression is False (avoid side effects)
- Performance: O(n) where n is number of expression pairs
When to Use Switch vs Alternatives
- Use Switch when you have multiple conditions to check and want to return a value
- Use Select Case for complex branching logic or when executing statements rather than returning values
- Use
IIffor simple two-way decisions - Use nested
IIfcautiously (Switch is clearer for 3+ conditions)
Typical Uses
- Conditional Value Selection: Return different values based on multiple conditions
- Grade Calculation: Assign letter grades based on numeric scores
- Status Messages: Return appropriate messages based on state
- Categorization: Categorize data based on multiple criteria
- Default Values: Provide values with fallback logic
- Lookup Logic: Implement simple lookup tables with conditions
- Data Transformation: Transform data based on multiple rules
- Validation Messages: Return validation results based on checks
Basic Examples
Example 1: Simple Value Selection
Dim result As Variant
Dim score As Integer
score = 85
result = Switch(score >= 90, "A", _
score >= 80, "B", _
score >= 70, "C", _
score >= 60, "D", _
True, "F") ' Default case
' result = "B"
Example 2: Status Messages
Function GetStatusMessage(status As Integer) As String
GetStatusMessage = Switch( _
status = 0, "Idle", _
status = 1, "Processing", _
status = 2, "Complete", _
status = 3, "Error", _
True, "Unknown")
End Function
Example 3: Range-Based Categorization
Function CategorizeAge(age As Integer) As String
CategorizeAge = Switch( _
age < 13, "Child", _
age < 20, "Teenager", _
age < 65, "Adult", _
True, "Senior")
End Function
Example 4: Multiple Condition Checks
Dim priority As String
Dim isUrgent As Boolean
Dim isImportant As Boolean
Dim hasDeadline As Boolean
priority = Switch( _
isUrgent And isImportant, "Critical", _
isUrgent, "High", _
isImportant, "Medium", _
hasDeadline, "Low", _
True, "Backlog")
Common Patterns
Pattern 1: Grade Assignment
Function GetLetterGrade(score As Double) As String
GetLetterGrade = Switch( _
score >= 90, "A", _
score >= 80, "B", _
score >= 70, "C", _
score >= 60, "D", _
True, "F")
End Function
Pattern 2: Day of Week Name
Function GetDayName(dayNum As Integer) As String
GetDayName = Switch( _
dayNum = 1, "Sunday", _
dayNum = 2, "Monday", _
dayNum = 3, "Tuesday", _
dayNum = 4, "Wednesday", _
dayNum = 5, "Thursday", _
dayNum = 6, "Friday", _
dayNum = 7, "Saturday", _
True, "Invalid")
End Function
Pattern 3: Conditional Formatting
Function GetColorCode(value As Double, threshold1 As Double, threshold2 As Double) As Long
GetColorCode = Switch( _
value < threshold1, vbRed, _
value < threshold2, vbYellow, _
True, vbGreen)
End Function
Pattern 4: Fee Calculation
Function CalculateShippingFee(weight As Double) As Currency
CalculateShippingFee = Switch( _
weight <= 1, 5.99, _
weight <= 5, 9.99, _
weight <= 10, 14.99, _
weight <= 20, 24.99, _
True, 39.99)
End Function
Pattern 5: Error Level Description
Function GetErrorDescription(errorLevel As Integer) As String
GetErrorDescription = Switch( _
errorLevel = 0, "No error", _
errorLevel = 1, "Warning", _
errorLevel = 2, "Error", _
errorLevel = 3, "Critical error", _
errorLevel = 4, "Fatal error", _
True, "Unknown error level")
End Function
Pattern 6: Conditional Default Value
Function GetConfigValue(key As String, userValue As Variant, defaultValue As Variant) As Variant
GetConfigValue = Switch( _
Not IsNull(userValue), userValue, _
True, defaultValue)
End Function
Pattern 7: Multi-Field Validation
Function ValidateRecord(name As String, age As Integer, email As String) As String
ValidateRecord = Switch( _
Len(name) = 0, "Name is required", _
age < 0 Or age > 120, "Invalid age", _
InStr(email, "@") = 0, "Invalid email", _
True, "Valid")
End Function
Pattern 8: Price Tier Selection
Function GetPriceTier(quantity As Integer) As String
GetPriceTier = Switch( _
quantity >= 1000, "Enterprise", _
quantity >= 100, "Business", _
quantity >= 10, "Professional", _
True, "Individual")
End Function
Pattern 9: File Type Detection
Function GetFileType(extension As String) As String
Dim ext As String
ext = LCase$(extension)
GetFileType = Switch( _
ext = "txt" Or ext = "log", "Text", _
ext = "doc" Or ext = "docx", "Word", _
ext = "xls" Or ext = "xlsx", "Excel", _
ext = "jpg" Or ext = "png" Or ext = "gif", "Image", _
True, "Unknown")
End Function
Pattern 10: Temperature Category
Function DescribeTemperature(tempF As Double) As String
DescribeTemperature = Switch( _
tempF < 32, "Freezing", _
tempF < 50, "Cold", _
tempF < 70, "Cool", _
tempF < 85, "Comfortable", _
tempF < 100, "Hot", _
True, "Very Hot")
End Function
Advanced Usage
Example 1: Dynamic Selector Class
' Class: DynamicSelector
' Provides advanced selection logic using Switch
Option Explicit
Private m_Criteria As Collection
Public Sub Initialize()
Set m_Criteria = New Collection
End Sub
Public Sub AddCriteria(expression As Boolean, value As Variant)
Dim pair As New Collection
pair.Add expression
pair.Add value
m_Criteria.Add pair
End Sub
Public Function Evaluate() As Variant
Dim item As Variant
Dim expr As Boolean
Dim value As Variant
For Each item In m_Criteria
expr = item(1)
If expr Then
If IsObject(item(2)) Then
Set Evaluate = item(2)
Else
Evaluate = item(2)
End If
Exit Function
End If
Next item
Evaluate = Null
End Function
Public Function EvaluateWithDefault(defaultValue As Variant) As Variant
EvaluateWithDefault = Switch( _
Not IsNull(Evaluate()), Evaluate(), _
True, defaultValue)
End Function
Example 2: Conditional Formatter Module
' Module: ConditionalFormatter
' Formats values based on multiple conditions
Option Explicit
Public Function FormatCurrency(amount As Currency, showSymbol As Boolean) As String
Dim formatted As String
formatted = Switch( _
amount < 0, "(" & Format$(Abs(amount), "#,##0.00") & ")", _
amount = 0, "0.00", _
True, Format$(amount, "#,##0.00"))
If showSymbol Then
FormatCurrency = "$" & formatted
Else
FormatCurrency = formatted
End If
End Function
Public Function FormatPercentage(value As Double, decimals As Integer) As String
Dim format As String
format = Switch( _
decimals = 0, "0%", _
decimals = 1, "0.0%", _
decimals = 2, "0.00%", _
True, "0.00%")
FormatPercentage = Format$(value * 100, format)
End Function
Public Function FormatFileSize(bytes As Long) As String
FormatFileSize = Switch( _
bytes < 1024, bytes & " B", _
bytes < 1048576, Format$(bytes / 1024, "0.0") & " KB", _
bytes < 1073741824, Format$(bytes / 1048576, "0.0") & " MB", _
True, Format$(bytes / 1073741824, "0.0") & " GB")
End Function
Public Function GetStatusColor(status As String) As Long
GetStatusColor = Switch( _
status = "Active", vbGreen, _
status = "Pending", vbYellow, _
status = "Inactive", vbGray, _
status = "Error", vbRed, _
True, vbBlack)
End Function
Example 3: Business Rules Engine
' Class: BusinessRulesEngine
' Evaluates business rules using Switch
Option Explicit
Public Function CalculateDiscount(customerType As String, orderAmount As Currency, _
quantity As Integer) As Double
' Return discount percentage
CalculateDiscount = Switch( _
customerType = "VIP" And orderAmount > 1000, 0.2, _
customerType = "VIP", 0.15, _
orderAmount > 5000, 0.15, _
orderAmount > 1000, 0.1, _
quantity > 100, 0.1, _
quantity > 50, 0.05, _
True, 0)
End Function
Public Function DetermineShippingMethod(weight As Double, destination As String, _
isExpress As Boolean) As String
DetermineShippingMethod = Switch( _
isExpress And weight < 5, "Express Air", _
isExpress, "Express Ground", _
destination = "International", "International Standard", _
weight > 50, "Freight", _
weight > 10, "Ground", _
True, "Standard Mail")
End Function
Public Function CalculateTax(amount As Currency, state As String, _
category As String) As Currency
Dim taxRate As Double
taxRate = Switch( _
state = "CA" And category = "Food", 0, _
state = "CA", 0.0725, _
state = "NY", 0.08, _
state = "TX", 0.0625, _
state = "FL", 0.06, _
True, 0.05)
CalculateTax = amount * taxRate
End Function
Public Function ApprovalRequired(amount As Currency, department As String, _
requestor As String) As Boolean
ApprovalRequired = Switch( _
amount > 10000, True, _
amount > 5000 And department <> "Finance", True, _
amount > 1000 And requestor = "Junior", True, _
True, False)
End Function
Example 4: Data Categorizer Module
' Module: DataCategorizer
' Categorizes data based on complex rules
Option Explicit
Public Function CategorizeCustomer(totalPurchases As Currency, _
yearsMember As Integer, _
lastPurchaseDays As Integer) As String
CategorizeCustomer = Switch( _
totalPurchases > 10000 And yearsMember > 5, "Platinum", _
totalPurchases > 5000 And yearsMember > 3, "Gold", _
totalPurchases > 1000 Or yearsMember > 2, "Silver", _
lastPurchaseDays < 90, "Active", _
lastPurchaseDays < 365, "Inactive", _
True, "Dormant")
End Function
Public Function AssignPriority(severity As Integer, impact As Integer, _
urgency As Integer) As String
AssignPriority = Switch( _
severity = 1 And impact = 1, "P1 - Critical", _
severity <= 2 And impact <= 2 And urgency = 1, "P2 - High", _
severity <= 3 Or impact <= 3, "P3 - Medium", _
urgency <= 3, "P4 - Low", _
True, "P5 - Backlog")
End Function
Public Function DetermineRiskLevel(score As Integer, volatility As Double, _
exposure As Currency) As String
DetermineRiskLevel = Switch( _
score < 300 Or exposure > 1000000, "High Risk", _
score < 500 Or volatility > 0.3, "Medium Risk", _
score < 700 And volatility > 0.15, "Low-Medium Risk", _
True, "Low Risk")
End Function
Public Function GetAgeGroup(age As Integer) As String
GetAgeGroup = Switch( _
age < 2, "Infant", _
age < 13, "Child", _
age < 20, "Teenager", _
age < 40, "Young Adult", _
age < 65, "Adult", _
True, "Senior")
End Function
Error Handling
The Switch function can raise the following errors:
- Error 450 (Wrong number of arguments or invalid property assignment): If arguments are not provided in pairs (odd number of arguments)
- Error 13 (Type mismatch): If expressions cannot be evaluated as Boolean
- Error 94 (Invalid use of Null): In some contexts when returned Null is used without checking
Performance Notes
- Evaluates expressions left to right until True is found (short-circuit)
- More efficient than nested
IIffor multiple conditions - All value expressions may be evaluated regardless of condition (avoid side effects)
- For large numbers of conditions (10+), Select Case may be more readable
- Performance: O(n) where n is the number of condition/value pairs
Best Practices
- Always provide default case using
Trueas the last condition to avoid Null returns - Order conditions properly - most specific first, most general last
- Avoid side effects in value expressions (they may all be evaluated)
- Use for readability - Switch is clearer than nested
IIffor 3+ conditions - Check for Null when using returned value if no default case provided
- Keep pairs aligned for readability (use line continuation)
- Limit complexity - if more than 7-8 pairs, consider Select Case
- Document complex logic when conditions are not self-evident
- Test all branches to ensure correct behavior
- Consider Select Case for executing statements vs returning values
Comparison Table
| Construct | Type | Branches | Returns Value | Short-Circuit |
|---|---|---|---|---|
Switch |
Function | Multiple | Yes | Yes (conditions) |
IIf |
Function | 2 | Yes | No |
Select Case |
Statement | Multiple | No (use with function) | Yes |
If...Then...Else |
Statement | Multiple | No (use with function) | Yes |
Platform Notes
- Available in VB6, VBA, and
VBScript - Behavior consistent across platforms
- Return type is always Variant
- Arguments must come in pairs (expression, value)
- Null return when no condition is True (not an error)
Limitations
- Must have even number of arguments (pairs)
- Cannot execute statements (only return values)
- Returns Variant (must convert if specific type needed)
- All value expressions may be evaluated (cannot assume short-circuit for values)
- Less readable than Select Case for complex branching
- No fall-through behavior like some languages' switch statements
- Cannot use ranges directly (must use comparison expressions)