The Select Case Statement

Now that you’re familiar with IF ELSE IF and END IF blocks for branching your code, we’ll take a look at a much neater and more flexible alternative in the SELECT CASE Statement.  If you have a good memory and were paying attention (often mutually exclusive), you’ll remember how we use the IF Statement in Visual Basic to branch the execution of our code depending on certain conditions which we specify.

Open up the “If Statement” project again.   View the code in the button click event (browse to the form in design view and double click on the “Younger or Older” button.

If CInt(TextBoxAge.Text) < 3 Then
     MsgBox("You are younger than Chris' niece")
ElseIf CInt(TextBoxAge.Text) < 3 Then
     MsgBox("You are older than Chris' niece")
Else
     MsgBox("You are 3, the same age as Chris' niece")
End If

This code works perfectly well, however you have to read each IF/ELSE IF statement there is a clearer and easier to read means of implementing the same code. Never underestimate the importance of having code that is easy to read. Heck, the reason I chose Visual Basic as a language is that is was, and is, much easier to read than other languages. Making your Visual Basic code easy to read will make it much easier for you to fix or enhance your code particularly at a later date. If you end up being a developer in a project team your co-workers will thank you for this. You have made the code easier to maintain for both you and your co-workers. It may sound like an outlandish claim but making your code easy to read makes you far more popular and employable!  Wow!

To make this code easier to maintain we can use the SELECT CASE statement.

Replace the code with this

Dim age As Integer

age = CInt(TextBoxAge.Text)

Select Case age
     Case Is < 3
         MsgBox("You are younger than Chris' niece")
     Case Is > 3
         MsgBox("You are older than Chris' niece")
     Case Is = 3
         MsgBox("You are 3, the same age as Chris' niece")
End Select

Run the code, it operates in exactly the same fashion. But what is actually happening here?

The Select Case Statement in Visual Basic statement has the following syntax

SELECT CASE EXPRESSION/VARIABLE

     CASE condition
          execute if condition is true

     CASE othercondition
          execute if othercondition is true

     CASE ELSE
          execute if all other conditions fail

END SELECT

Diagrammatically, the statement looks as follows.

select case

Conceptually it’s exactly the same as the IF, ELSE IF, ELSE END IF block we looked at in the last tutorial.   But hopefully you can see the code looks much cleaner.   For one, there’s fewer words.  The expression we are evaluating is stated at the top of the SELECT CASE block, rather than on every line.

Instead of asking

ElseIf CInt(TextBoxAge.Text) < 3 Then

We ask

Case Is < 3

We didn’t have to ask what < 3 related to as we specified that in the header of the SELECT CASE statement.    It’s a less repetitive way of asking the same question.    It’s less wordy. It’s more elegant.  I’ve seen these CASE statements going on 20 or 30 times and it makes the code look much much cleaner.

There are performance advantages in utlilising the SELECT CASE STATEMENT but I wouldn’t use that as the primary means of using it.  Expressions like the IF Statement and SELECT CASE statement are typically executed in millionths of a second these days.  Not even your most critical of users will notice the difference.  However it is simply much easier to read.  You should always, without exception, try to keep your code as clean and as elegant as possible.  Your co-workers will thank you for it.  You will thank yourself for it when you go to look at some old code.  Take pride in your work!  Be a good craftsman.