Switch Statement

Select Case is a VB.NET selection statement. It quickly matches the value of a variable with a set of constants in a VB.NET program.

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


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


Multiple 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

String Example:

 Dim c As String
   c = TextBox1.Text
   Select c
   Case "Red"
      MsgBox("Color code of Red is::#FF0000")
   Case "Green"
      MsgBox("Color code of Green is::#808000")
   Case "Blue"
      MsgBox("Color code of Blue is:: #0000FF")
   Case Else
      MsgBox("Enter correct choice")
   End Select
 End Sub

No comments:

Post a Comment