Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
Visual Basic Select Case Statement
Visual Basic Select Case Statement
In Visual Basic, Select...Case statement is useful to execute a single case statement from the group of multiple case statements based on the value of a defined expression.
By using Select...Case statement in Visual Basic, we can replace the functionality of if…else if statement to provide better readability for the code.
Visual Basic Select Case Statement Syntax
Generally, in Visual Basic, the Select...Case statement is a collection of multiple case statements, and it will execute only one case statement based on the matching value of the defined expression.
Following is the syntax of defining the Select...Case statement in Visual Basic programming language.
Case value1
// Statements to Execute
Case value2
//Statements to Execute
....
....
Case Else
// Statements to Execute if No Case Matches
End Select
If you observe the above syntax, we defined a Select...Case statement with multiple case statements. The Select statement will evaluate the expression / variable value by matching with Case statement values (value1, value2, etc.). If the variable/expression value matches with any of the case statements, the statements inside of that particular case will be executed.
If none of the case statements match the defined expression/variable value, then the statements inside the Else block will be executed, and it’s more like Else block in the if...else statement.
Visual Basic Select Case Statement Flow Chart
Following is the pictorial representation of Select...Case statement process flow in Visual Basic programming language.
If you observe the above Select...Case statement flow chart diagram, the process flow of Select..Case statement will start from the Top to the Bottom, and in the first case, it will check whether the expression value matches or not.
In case, if the expression value matches, it will execute the particular Case statement block and execute the Select statement; otherwise, it will go to the second Case statement and check whether the expression value is matching or not, the same way the search will continue till it finds the right Case statement.
If all case statements fail to match with the defined expression value, then the Else block statements will be executed, and the Select statement will come to an end.
Visual Basic Select Case Statement Example
Following is the example of using select...case statement in Visual Basic programming language.
Sub Main()
Dim x As Integer = 20
Select Case x
Case 10
Console.WriteLine("x value is 10")
Case 15
Console.WriteLine("x value is 15")
Case 20
Console.WriteLine("x value is 20")
Case Else
Console.WriteLine("Not Known")
End Select
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
If you observe the above example, we defined Select with multiple Case statements, and it will execute the matched Case statements with the expression value.
When we execute the above Visual Basic program, we will get the result as shown below.
If you observe the above result, the Case statement (20) matches the defined expression value (20) and executes the statements within the respective Case statement.
Visual Basic Nested Select Case Statements
In Visual Basic, using one Select...Case statement within another Select...Case statement is called a nested Select...Case statements.
Following is the example of using nested Select...Case statements in Visual Basic programming language.
Sub Main()
Dim x As Integer = 10, y As Integer = 5
Select Case x
Case 10
Console.WriteLine("X Value: 10")
Select Case y
Case 5
Console.WriteLine("Nested Switch Value: 5")
Select Case y - 2
Case 3
Console.WriteLine("Another Nested Switch Value: 3")
End Select
End Select
Case 15
Console.WriteLine("X Value: 15")
Case 20
Console.WriteLine("X Value: 20")
Case Else
Console.WriteLine("Not Known")
End Select
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
If you observe the above example, we used Select...Case statements within another Select...Case statements to implement nested Select...Case statements based on our requirements.
When we execute the above Visual Basic program, we will get the result as shown below.
If you observe the above result, the nested Select...Case statements have been executed based on our requirements.
Visual Basic Select Case Statement with Enum
In Visual Basic, we can use enum values with Select...Case statements to perform the required operations.
Following is the example of using enum values in the c# switch case statement.
Sub Main()
Dim loc As location = location.hyderabad
Select Case loc
Case location.chennai
Console.WriteLine("Location: Chennai")
Case location.guntur
Console.WriteLine("Location: Guntur")
Case location.hyderabad
Console.WriteLine("Location: Hyderabad")
Case Else
Console.WriteLine("Not Known")
End Select
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
Public Enum location
hyderabad
chennai
guntur
End Enum
End Module
If you observe the above example, we defined enum values and used those values in Select Case statements to perform the required operations based on our requirements.
When we execute the above Visual Basic program, we will get the result as shown below.
If you observe the above result, the Select Case statement which matches the enum value has been printed in the console window.
This is how we can use the enums with Select case statements in Visual Basic to perform the operations based on our requirements.