Select Case statements are not very different from If Statements. In fact, all Select Case Statements can be written using an If statement, although not as easily. For example, we could write:

If UserInput = 0 Then
  'Do something.
ElseIf UserInput = 1 Then
  'Do something else.
ElseIf UserInput = 2 Then
  'Do something again.
ElseIf UserInput = 3 Then
  'Do something yet again.
Else
  'Do something one last time.
End If

However, as you can see, this can get tiring fast. Using a Select Case statement, we can make this code much less repetitive and easier to read:

Select Case UserInput

  Case 0
    'Do something.
  Case 1
    'Do something else.
  Case 2
    'Do something again.
  Case 3
    'Do something yet again.
  Case Else
    'Do something one last time.

End Select

The Select Case statements can compare Strings, Chars, Dates, Boolean, and any number type of object. If needed, you can even have multiple condition checks (similar to the If statements "Or") by adding a comma between the values to check. For example:

Select Case UserInput

  Case 0
    'Do something.
  Case 1,2,3
    'Do something else.
  Case Else
    'Do something one last time.

End Select

Next, we will begin discussing how to use loops in VB.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!