Home >>VB.Net Programs >VB.Net program to demonstrate the use of 'select case'
Here, by entering the user's selection and then printing the appropriate message on the console screen, we select a choice using 'select case'.
Program :
The source code to show the use of "select case" is given below. The program given is compiled and successfully executed.
Example
Module Module1
Sub Main()
Dim choice As Char
Console.WriteLine("*************************")
Console.WriteLine(" a: India")
Console.WriteLine(" b: Australia")
Console.WriteLine(" c: USA")
Console.WriteLine(" d: UK")
Console.WriteLine("*************************")
Console.Write("Select your country: ")
choice = Char.Parse(Console.ReadLine())
Select Case choice
Case "a"
Console.WriteLine("India")
Case "b"
Console.WriteLine("Australia")
Case "c"
Console.WriteLine("USA")
Case "d"
Console.WriteLine("UK")
Case Else
Console.WriteLine("Invalid choice")
End Select
End Sub
End Module
Explanation:
We created a Module1 module in the program above that contains the Main() method. We created a variable choice for the Char type in the Main() method.
Select Case choice Case "a" Console.WriteLine("India") Case "b" Console.WriteLine("Australia") Case "c" Console.WriteLine("USA") Case "d" Console.WriteLine("UK") Case Else Console.WriteLine("Invalid choice") End Select
We used the select case in the above code, where we defined multiple cases according to the choices given. If the wrong choice is entered, which is not given, then "Case Else" will be executed.