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- expression is the variable or expression whose value you want to evaluate.
- Case is followed by a value to check against expression.
- You can have multiple Case sections to handle different values.
- Case Else is optional and handles the situation when none of the previous cases match.
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.NetCheck 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:
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.
- Hidden Features of VB.Net
- Check if program is running in VB.Net
- Determine the size of a structure | VB.Net
- How to remove decimal from variable in VB.Net
- VB.Net wait (x) seconds
- How to Get a File Extension Using VB.NET
- VB.NET ToUpper Examples
- Local Variables in VB.Net
- VB.Net DateOnly and TimeOnly
- VB.Net - Get the byte size of type
- Concatenation Operators in VB.Net
- Ternary operator in VB.NET
- Difference between And and AndAlso in VB.Net
- Difference between + and & for joining strings in VB.Net
- Reference to a non-shared member requires an object reference