Select Case Statements

The Select Case statement is another way to test what is inside of a variable. You can use it when you know there is only a limited number of things that could be in the variable. For example, suppose we add another choice for that cream cake. We've only said that the consequences of eating the cream cake is that the Diet will be either "Ruined" or "Not Ruined". But what if we add another option - "Diet Not Tested". In other words, we ate the cake but refused to climb onto the scales to weigh ourselves!

With three choices, we can still use an If ... Else statement. But let's change it to a Select Case statement. Remember: all we are doing is testing what is inside a variable, in this case a variable called creamcake. Once we decide what is inside the variable, we can take some action. So let's look at how the Select Case works.

Create a Form with a button and a Textbox on it (If you have your form open from the previous section, then you can use this one). Double click the new button. You should see something like this appear.

Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click

End Sub

Between the button Sub and End Sub code add the folowing

Dim creamcake As String
Dim DietState As String

creamcake = TextBox1.Text

Select Case creamcake

Case "Eaten"
DietState = "Diet Ruined"
Case "Not Eaten"
DietState = "Diet Not Ruined"
Case Else
DietState = "Didn't check"

End Select

MessageBox.Show(DietState)

Run your code to test it out. Click inside your textbox and enter the word "Eaten". Then click your button to see what happens. Now enter the words "Not Eaten" and click your button. Next, try the word "eaten", with a lowercase "e".

So, how does the Select case work?

In the code above, we first set up two variables called creamcake and DietState. Next, we transfer whatever is in Textbox1 to the variable creamcake. The Select Case begins on the next line:

Select Case creamcake

We tell Visual Basic that we want to start a Select Case statement by simply using the words "Select Case". This is enough to set up the statement. The variable creamcake follows the words Select Case. We're saying, "Set up a Select Case statement to test what is inside the variable called creamcake". The next line is this:

Case "Eaten"

We ask Visual Basic to check if the variable creamcake contains the word "Eaten". (Is it the Case that ... ?)

If it is the Case that creamcake contains the word "Eaten", then VB will drop down to the line or lines of code below and read that. If the variable creamcake doesn't contain the word "Eaten", the programme will skip the line or lines of code below and jump to the next Case.

The programme will continue to check all the words after Case to see if one of them contains what is in the variable creamcake. If it finds one, it will read the code below the Case word; if it doesn't find any matches, it will not do anything at all. In our code, we're checking these three Cases:

Case "Eaten"
Case"Not Eaten"
Case Else

Note also that it will only look for an exact match - "Eaten", but not "eaten".

The next line to examine is this:

Case Else

You can use the Else word after Case. If the programme hasn't found any matches, it will then execute the code below the Case Else line.

The final line to examing is this:

End Select

All we're doing here is to tell Visual basic to end the Select Case statement.

 

 

So the Select Case checks a variable for any number of different choices. If a match is found, it will then execute code below the Case option it has found. In our code, we've just told the programme to store some text in the DietState variable. After the Select Case statement has ended we displayed the variable in a Message Box.

You can use Select Case statement with numbers, as well, and the To word comes in very handy here. If you were checking a variable to see if the number that was in the variable fell within a certain age-range, you could use something like this:

Select Case agerange

Case 16 To 21
MessageBox.Show(“Still Young”)
Case 50 To 64
MessageBox.Show(“Start Lying”)

End Select

Here, a variable called agerange is being tested. It's being checked to see if it falls between certain values. If it does, then a message box is displayed.

 

Exercise G

Add a new button to your form. Write a programme that tests if a person is a) A teenager, b) in their twenties, c) in their thirties, or d) none of the above.

Answer to Exercise G

 

A popular way to use Select Case statements is with a drop down box. You can then test which item a user selected from a list of available items. You're going to write a little programme to do just this. But before you can do so, you'll need to know how to add a Combo Box to a form, and how to get at the values in its list. We'll do that in the next section.