Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the use of 'select case'
By Nidhi Last Updated : November 14, 2024
The "select case" in VB.Net
Here, we will select an option using "select case" by entering the choice from the user and then print the appropriate message on the console screen.
VB.Net code to demonstrate the use of 'select case'
The source code to demonstrate the use of "select case" is given below. The given program is compiled and executed successfully.
'VB.Net program to demonstrate the "select case".
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
Output:
############################
a: India
b: Australia
c: USA
d: UK
############################
Select your country: a
India
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1 that contains a Main() method. In the Main() method, we created a variable choice of Char type.
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())
In the above code, we read a character for selection.
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
In the above code, we used select case, here we defined multiple cases according to given choices. If we entered the wrong choice, which is not given then "Case Else" will be executed.
VB.Net Basic Programs »