Visual Basic .NET - Select Case Statement
In VB.NET, the Select Case statement is used to create a switch-case structure. It allows you to execute different code blocks based on the value of a variable or an expression.
Select Case variable
Case value1
' code to execute if variable equals value1
Case value2
' code to execute if variable equals value2
Case Else
' code to execute if variable does not match any of the above cases
End Select
Here is an example of a Select Case statement in action:
Dim dayOfWeek As Integer = 3
Select Case dayOfWeek
Case 1
Console.WriteLine("Sunday")
Case 2
Console.WriteLine("Monday")
Case 3
Console.WriteLine("Tuesday")
Case 4
Console.WriteLine("Wednesday")
Case 5
Console.WriteLine("Thursday")
Case 6
Console.WriteLine("Friday")
Case 7
Console.WriteLine("Saturday")
Case Else
Console.WriteLine("Invalid day")
End Select
In this example, we have a variable called dayOfWeek with a value of 3. The Select Case statement checks the value of dayOfWeek and executes the code block that corresponds to the matching case. In this case, the output would be "Tuesday".
If none of the cases match the value of the variable, the Case Else block is executed. This is similar to the default case in a switch-case structure in other programming languages.