Select Case

A Select Case is used when you want to take an action based on the value of a variable.

With the If-Then, we were limited to two actions based on the truth of a condition (unless we adopted the more complex Nested If-Then or the ElseIf).

Here, we put it into a structure that is a bit simpler to read.

The syntax for a Select Case is:

Select Case <value/variable to test>

Case <test value>

Case Else

End Select

You can have as many Case statements as you wish, but only one Case Else.  Each case is one or more comparison values.  The Case Else is used to execute code when all the other cases fail.

Example 2 from the previous lecture would actually work better with this code structure.

Select Case intGrade

Case Is > 90

lblGrade.Caption = "A"

Case Is > 80

lblGrade.Caption = "B"

Case Is > 70

lblGrade.Caption = "C"

Case Is > 60

lblGrade.Caption = "D"

Case Else

lblGrade.Caption = "F"

End Select