Now that you’re familiar with using the IF ELSE IF and END IF statements to branch 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 “Calculator” project again. View the code in the button click event (browse to the form in design view and double click on the “Calculate” button.
If CInt(TextBoxAge.Text) < 3 Then MessageBox.Show("You are younger than Chris' niece") ElseIf CInt(TextBoxAge.Text) < 3 Then MessageBox.Show("You are older than Chris' niece") Else MessageBox.Show("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 are conditions to read in each block. There’s a whole lot of text there! Programmers hate text. There is a clearer and easier to read means of implementing the same code by using the SELECT CASE statement. Never underestimate the importance of having code that is easy to read. Heck, the reason I chose Visual Basic as a language is that it 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 when you’ve forgot exactly how it works. Whitespace is your friend!
To reduce the amount of text in this code and make this code easier to read, replace the code with this:
Dim age As Integer age = CInt(TextBoxAge.Text) Select Case age Case Is < 3 MessageBox.Show("You are younger than Chris' niece") Case Is > 3 MessageBox.Show("You are older than Chris' niece") Case Is = 3 MessageBox.Show("You are 3, the same age as Chris' niece") End Select
Run the code and you’ll see the exact same results. 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:
Conceptually it’s exactly the same as the IF, ELSE IF, ELSE END IF block we looked at previously. But the code looks much cleaner. There are fewer words. It’s visually less cluttered and easier to digest. There’s more white space. All top designers use white space to make things look more beautiful. Web designers, architects, landscape gardeners, musicians, artists and poets all understand how to use white space to make things look more visually appealing. Programming is no exception. The Select Case Statement is a great example of making your code look better which in turn makes it easier to digest and maintain.
The reason for the extra white space is that 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
Case Is < 3
We didn’t have to ask what ’+’ related to as we specified that in the header of the SELECT CASE block. 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 cleaner.
There are performance advantages in utilising 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.