Select Case Statement in VB.Net

The Select Case statement in VB.NET is a control flow statement that allows you to execute different code blocks based on the value of a variable.

The Select Case statement works by checking the value of a variable against a list of possible values. If the value of the variable matches one of the possible values, the corresponding code block is executed. If the value of the variable does not match any of the possible values, the default code block is executed.

Select Case statement

The Select Case statement is useful for implementing decision logic in your VB.NET code.

Syntax
Select Case expression Case value1 ' Code to execute when expression equals value1 Case value2 ' Code to execute when expression equals value2 ' Additional cases... Case Else ' Code to execute if none of the above cases match End Select
Explanation:
  1. expression is the variable or expression whose value you want to evaluate.
  2. Case is followed by a value to check against expression.
  3. You can have multiple Case sections to handle different values.
  4. Case Else is optional and handles the situation when none of the previous cases match.
Example:
Dim dayOfWeek As Integer = 3 Dim dayName As String Select Case dayOfWeek Case 1 dayName = "Sunday" Case 2 dayName = "Monday" Case 3 dayName = "Tuesday" Case 4 dayName = "Wednesday" Case 5 dayName = "Thursday" Case 6 dayName = "Friday" Case 7 dayName = "Saturday" Case Else dayName = "Invalid day" End Select Console.WriteLine($"Day of the week is {dayName}.")

In this example, the Select Case statement is used to determine the day of the week based on the value of dayOfWeek. It assigns the corresponding day name to the dayName variable, and the result is displayed using Console.WriteLine.

Full Source| VB.Net
Imports System Module Program Sub Main() Dim dayOfWeek As Integer = 3 Dim dayName As String Select Case dayOfWeek Case 1 dayName = "Sunday" Case 2 dayName = "Monday" Case 3 dayName = "Tuesday" Case 4 dayName = "Wednesday" Case 5 dayName = "Thursday" Case 6 dayName = "Friday" Case 7 dayName = "Saturday" Case Else dayName = "Invalid day" End Select Console.WriteLine("Day of the week is :" & dayName) End Sub End Module

Check for ranges of values

You can also use the Select Case statement to check for ranges of values. For example, the following code uses the Select Case statement to check if a variable named temperature is between 0 and 10 degrees Celsius:

Dim temperature As Integer = 5 Select Case temperature Case Is >= 0 And temperature < 10 Console.WriteLine("The temperature is between 0 and 10 degrees Celsius.") Case Else Console.WriteLine("The temperature is outside the range of 0 and 10 degrees Celsius.") End Select ' Output: The temperature is between 0 and 10 degrees Celsius.

Conclusion

The Select Case statement is used for conditional branching, allowing you to evaluate an expression and choose from multiple execution paths based on its value. It simplifies complex branching logic by providing a clear and organized way to handle different cases or conditions, making your code more readable and maintainable.