SELECT CASE statement in Visual Basic .Net

How you are going to solve this problem in Visual Basic

If day is Monday print “Go Hiking”
If day is Tuesday print “Go Shopping”
If day is Wednesday print “Watch Movie”
If day is Thursday print “Play Games”
If day is Friday print “Have a drink”

I can write a program using If…Then statements like this

Dim day As String = "Friday"
If day = "Monday" Then
Console.WriteLine("Go Hiking")
ElseIf day = "Tuesday" Then
Console.WriteLine("Go Shopping")
ElseIf day = "Wednesday" Then
Console.WriteLine("Watch Movie")
ElseIf day = "Thursday" Then
Console.WriteLine("Play Games")
ElseIf day = "Friday" Then
Console.WriteLine("Have a Drink")
End If

I am now using Select Case method to implement the same

Dim day As String = "Friday"
Select Case day
Case "Monday"
Console.WriteLine("Go Hiking")
Case "Tuesday"
Console.WriteLine("Go Shopping")
Case "Wednesday"
Console.WriteLine("Watch Movie")
Case "Thursday"
Console.WriteLine("Play Games")
Case "Friday"
Console.WriteLine("Have a Drink")
End Select

Here you can define separate condition for each case here. This method is very good to implement multiple If statement

If none of the case statements match with your condition you can get Case Else do something


Dim day As String = "Friday"
Select Case day
Case "Monday"
Console.WriteLine("Go Hiking")
Case "Tuesday"
Console.WriteLine("Go Shopping")
Case "Wednesday"
Console.WriteLine("Watch Movie")
Case "Thursday"
Console.WriteLine("Play Games")
Case "Friday"
Console.WriteLine("Have a Drink")
Case Else
Console.WriteLine("Nothing")
End Select

Leave a Reply

Your email address will not be published. Required fields are marked *