Select Case Statement in VB.NET

How to Write a Select Case Statement in


Select Case Statement

Select case statement is used when the expected results for a condition can be known previously so that different set of operations can be done based on each condition.

Select Case Expression
Case Expression1
Case Expression2
Case Expressionn
Case Else
End Select

In the above syntax, the value of the Expression is checked with Expression1..n to check if the condition is true. If none of the conditions are matched the statements under the Case Else is executed.

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
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

In the above example based on the color input in TextBox1, the color code for RGB colors are displayed, if the color is different then the statement under Case Else is executed. Thus we can easily execute the select case statement.

Visual Basic Tutorial

Ask Questions

Ask Question