Monday, November 26, 2012

How to use Switch case in Visual Basic

How to use Select case in Visual Basic

The switch case doesn’t exist in Visual Basic, is replaced by the select-case statement. The select case is a disguise of the IF statement.  Is not very powerful and I would not recommend it. But using the select-case (switch-case) is good sometimes to use it to make your code easy to read.
Is important to know that the select-case could only evaluate basic and elementary data types (Boolean, double, integer, uinteger, char, ….). reference :

Select...Case Statement (Visual Basic)

Basically, if you want to test an expression in a TextBox1, your select-case should always start like this:


            Select Case TextBox1.Text

               
                Case Else


            End Select



With basic example, I hope you will be able to use the select-case.

using the select-case (switch-case)  equal to something.





            Select Case TextBox1.Text

                Case "1"
                    MsgBox("one")

                Case "2"
                    MsgBox("one")

                Case "3"
                    MsgBox("three")

                Case Else
                    MsgBox("enter a value from 1 to 3 in the textbox")

            End Select



The previous select-case is 100% identical to the if-else statement here:


            If TextBox1.Text = "1" Then
                MsgBox("one")

            ElseIf TextBox1.Text = "2" Then
                MsgBox("one")

            ElseIf TextBox1.Text = "3" Then
                MsgBox("three")

            Else
                MsgBox("enter a value from 1 to 3 in the textbox")

            End If



Please note that the select-case acts like a if-elseif-else statement. The moment the program encounter the first valid expression, it runs whatever inside that valid expression and quits immediately.  In Visual Basic, you don’t need to insert any break expression like in Java or in C#.

use the is in the select case



            Select Case TextBox1.Text

                Case Is < 0
                    MsgBox("is cold")

                Case Is < 18
                    MsgBox("is getting acceptable")

                Case Is < 22
                    MsgBox("is confortable")

                Case Is < 30
                    MsgBox("is getting hot")

                Case Is < 35
                    MsgBox("oh my god")

                Case Is < 40
                    MsgBox("call 911")

                Case Else
                    MsgBox("please enter a valid number in textbox")

            End Select


The previous code is the same if you use the if-elseif-else statement:


    If TextBox1.Text < 0 Then
                MsgBox("is cold")
            ElseIf TextBox1.Text < 18 Then
                MsgBox("is getting acceptable")
            ElseIf TextBox1.Text < 22 Then
                MsgBox("is confortable")
            ElseIf TextBox1.Text < 30 Then
                MsgBox("is getting hot")
            ElseIf TextBox1.Text < 35 Then
                MsgBox("oh my god")
            ElseIf TextBox1.Text < 40 Then
                MsgBox("call 911")
            Else
                MsgBox("please enter a valid number in textbox")
            End If



Use a range using the TO function in the select-case (switch-case)



            Select Case TextBox1.Text

                Case -32000 To 0
                    MsgBox("is cold")

                Case 0 To 21
                    MsgBox("is getting acceptable")

                Case 22 To 30
                    MsgBox("is confortable")

                Case 31 To 34
                    MsgBox("is getting hot")

                Case 35 To 39
                    MsgBox("oh my god")

                Case 40 To 32000
                    MsgBox("call 911")

                Case Else
                    MsgBox("please enter a valid number in textbox")

            End Select



The previous with the use of the TO inside the select-case is equivalent to this code:


            If TextBox1.Text >= -32000 And TextBox1.Text < 0 Then
                MsgBox("is cold")
            ElseIf TextBox1.Text >= 0 And TextBox1.Text < 18 Then
                MsgBox("is getting acceptable")
            ElseIf TextBox1.Text >= 18 And TextBox1.Text < 22 Then
                MsgBox("is confortable")
            ElseIf TextBox1.Text >= 22 And TextBox1.Text < 30 Then
               MsgBox("is getting hot")
            ElseIf TextBox1.Text >= 30 And TextBox1.Text < 35 Then
                MsgBox("oh my god")
            ElseIf TextBox1.Text >= 35 And TextBox1.Text < 40 Then
                MsgBox("call 911")
            Else
                MsgBox("please enter a valid number in textbox")
            End If


Use multiples expressions


Use the comma to separate multiples expressions.


            Select Case TextBox1.Text

                Case -32000 To 0
                    MsgBox("is cold")

                Case 0, 1, 2, 3, 4, 5 To 21
                    MsgBox("is getting acceptable")

                Case 22 To 24, 25 To 30
                    MsgBox("is confortable")

                Case 31, 32, 33, 34
                    MsgBox("is getting hot")

                Case 35 To 39
                    MsgBox("oh my god")

                Case 40, 41, 42, 43, 44, 45, 46, 47, 48 To 32000
                    MsgBox("call 911")

                Case Else
                    MsgBox("please enter a valid number in textbox")

            End Select




Don’t forget to always put the case else statement in case your code goes wrong.

If you like this post, comment it or share it.

Visit my web site at http://checktechno.ca

2 comments:

  1. dim a as integer
    a = textbox1.text
    select case a
    case 0 to 50
    msgbox("F")
    case 50 to 60
    msgbox("E")
    case 60 to 70
    msgbox("D")
    case 70 to 80
    msgbox ("C")
    case 80 to 90
    msgbox ("B")
    case else
    msgbox("A")
    end select

    ReplyDelete