Lesson 14: Select Case in VB2019

Master efficient multi-condition handling with Select Case

Key Takeaway

The Select Case structure provides a cleaner and more efficient way to handle multiple conditions compared to complex If...Then...ElseIf statements.

In this lesson, we'll explore the Select Case control structure in Visual Basic 2019. While If...Then...Else is excellent for binary decisions, Select Case excels when you need to evaluate a single expression against multiple possible values.

14.1 Select Case Structure

The Select Case structure evaluates one expression and executes different code blocks based on matching values. It's ideal for scenarios with multiple discrete values to check.

Select Case Syntax

Select Case expression
    Case value1
        ' Code to execute when expression = value1
    Case value2
        ' Code to execute when expression = value2
    Case value3
        ' Code to execute when expression = value3
    Case Else
        ' Code to execute when no match is found
End Select

14.1(a) Basic Select Case

This structure executes specific code blocks based on exact matches with the expression value.

Select Case expression
Case matches execute specific code
Case Else handles unmatched values

Example 14.1: Grade Messages

This program displays a message associated with a grade entered by the user.

Form1.vb
Private Sub BtnShow_Click(sender As Object, e As EventArgs) Handles BtnShow.Click
    Dim grade As String
    grade = TxtGrade.Text
    
    Select Case grade
        Case "A"
            MsgBox("High Distinction")
        Case "A-"
            MsgBox("Distinction")
        Case "B"
            MsgBox("Credit")
        Case "C"
            MsgBox("Pass")
        Case Else
            MsgBox("Fail")
    End Select
End Sub

Output:

> Enter "A": "High Distinction" > Enter "B": "Credit" > Enter "F": "Fail"
Grade example interface
Figure 14.1: Grade example interface
Grade output
Figure 14.2: Grade output example

14.1(b) Using Comparison Operators

You can use the Is keyword with comparison operators to evaluate ranges of values.

Case Is >= 85
Execute code for values >=85

Example 14.2: Mark Evaluation

This program evaluates a mark and provides feedback using comparison operators.

Form1.vb
Private Sub BtnEvaluate_Click(sender As Object, e As EventArgs) Handles BtnEvaluate.Click
    Dim mark As Single
    mark = TxtMark.Text
    
    Select Case mark
        Case Is >= 85
            MsgBox("Excellence")
        Case Is >= 70
            MsgBox("Good")
        Case Is >= 60
            MsgBox("Above Average")
        Case Is >= 50
            MsgBox("Average")
        Case Else
            MsgBox("Need to work harder")
    End Select
End Sub

Output:

> Enter 90: "Excellence" > Enter 75: "Good" > Enter 65: "Above Average" > Enter 55: "Average" > Enter 45: "Need to work harder"

14.1(c) Using Value Ranges

Select Case allows you to specify ranges of values using the To keyword for more concise code.

Case 50 To 59
Execute code for values between 50-59

Example 14.3: Mark Ranges

This example demonstrates using value ranges for more concise code.

Form1.vb
Private Sub BtnEvaluate_Click(sender As Object, e As EventArgs) Handles BtnEvaluate.Click
    Dim mark As Single
    mark = TxtMark.Text
    
    Select Case mark
        Case 0 To 49
            MsgBox("Need to work harder")
        Case 50 To 59
            MsgBox("Average")
        Case 60 To 69
            MsgBox("Above Average")
        Case 70 To 84
            MsgBox("Good")
        Case 85 To 100
            MsgBox("Excellence")
        Case Else
            MsgBox("Wrong entry, please reenter the mark")
    End Select
End Sub

Output:

> Enter 45: "Need to work harder" > Enter 55: "Average" > Enter 65: "Above Average" > Enter 80: "Good" > Enter 95: "Excellence"

14.1(d) Calculating Grades

Select Case is ideal for converting numerical marks to letter grades.

Example 14.4: Grade Calculator

This program calculates a letter grade based on a numerical mark.

Form1.vb
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
    Dim mark As Single
    Dim grade As String
    mark = TxtMark.Text
    
    Select Case mark
        Case 0 To 49
            grade = "E"
        Case 50 To 59
            grade = "D"
        Case 60 To 69
            grade = "C"
        Case 70 To 79
            grade = "B"
        Case 80 To 100
            grade = "A"
        Case Else
            grade = "Error, please re-enter the mark"
    End Select
    
    LblGrade.Text = grade
End Sub

Output:

> Enter 45: Grade "E" > Enter 55: Grade "D" > Enter 65: Grade "C" > Enter 75: Grade "B" > Enter 85: Grade "A"
Grade calculator interface
Figure 14.3: Grade calculator interface

Lesson Summary

In this lesson, you've learned how to use the Select Case control structure for efficient multi-condition handling in Visual Basic 2019:

Select Case Structure

Mastered the fundamental Select Case...End Select syntax

Comparison Operators

Used the "Is" keyword with operators for range evaluations

Value Ranges

Implemented value ranges using the "To" keyword for concise code

Practical Applications

Built grade calculators and mark evaluation systems

Select Case provides a cleaner alternative to complex If...Then...ElseIf structures when evaluating a single expression against multiple possible values. In the next lesson, we'll explore looping structures to repeat code execution.

Next Lesson

Ready to learn about repeating code execution? Continue to Lesson 15: Looping.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon