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.
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.
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.
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.
Following is the example of using select...case statement in Visual Basic programming language.
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.
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.
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.
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.
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.