Select Case Statements

The other way to test the data inside a variable is by using a Select case Statements. In visual basic, the Select case statements are just like If statement but with a little difference. For me, the Select Case statement is better and more efficient. This is because every If statement must be executed one at a time, but the Select Case statement is only executed once and then it moves on to the next stage in the code. Sample Syntax
Select [ Case ] testexpression
    [ Case expressionlist
        [ statements ] ]
    [ Case Else
        [ elsestatements ] ]
End Select
This time, let’s apply this in the real-world application. Imagine that you want to watch a movie in a movie house. That time, there are five movies available, but before you choose the title of a movie, you want to know first what type of genre it is. To do this, open visual basic and create a new project then save it as “Select Case”. On the design view add a label, Combobox and a button. Then change the Text property of a label1 to “Movie Title.”, and for the button change the Name property into “bntshow” and the text property to “Show Genre”. Next for the Combobox1,change the Name property to “cbtitle” and fill items such as: Mission Impossible 4, Rio, Johnny English Reborn, The Vow and Immortals. Then arrange all the objects that look likes as shown below. This time, we’re going to add functionality to our application. To do this, double click the btnshow and add the following code:
  1. Dim movie As String
  3. movie = cbtitle.Text
  5. Select Case movie
  6. Case "Mission Impossible 4"
  7. MsgBox("The Genre of this movie is Action")
  8. Case "Rio"
  9. MsgBox("The Genre of this movie is Cartoons")
  10. Case "Johnny English Reborn"
  11. MsgBox("The Genre of this movie is Comedy")
  12. Case "The Vow"
  13. MsgBox("The Genre of this movie is Drama")
  14. Case "Immortals"
  15. MsgBox("The Genre of this movie is Adventures")
  16. Case Else
  18. MsgBox("I will watch movie at another time!")
  20. End Select
To test your program just press “F5”. The above example, we get the text value based on the user selected text. In our case we have a list of text, and these are Titles off movie. It simply checks if it is matched with our case labels then it will display a messagebox informing the type of movie genre. Select Case - using Operators Select Case using operators, we need to use the Is or To keyword. Example - Using To Keyword
  1. Dim agerange As Integer
  3. agerange = txtage.Text
  5. Select Case agerange
  6. Case 1 To 3
  7. MsgBox("Toddler")
  8. Case 5 To 12
  9. MsgBox("Childhood")
  10. Case 12 To 18
  11. MsgBox("Adolescence")
  12. Case Else
  13. MsgBox("Mature stage")
  15. End Select
Example - Using Is Keyword
  1. Dim agetovote As Integer
  3. agetovote = txtage.Text
  5. Select Case agetovote
  6. Case Is < 18
  7. MsgBox("Not ready to vote")
  8. Case Is >= 18
  9. MsgBox("Your ready to vote")
  10. Case Else
  11. MsgBox("Need More Information")
  13. End Select
The translation of this using Is keyword “Is Less than 18” and “Is Greater than or Equal to 18”, and for the translation by using To keyword “is between 1 and 3, 5 and 12 then 12 and 18”. And it is vivid that we can only use relational operators if you are evaluating an integer or double.

Add new comment