In a Select...Case
statement, is there a way to skip cases based on a precondition?
What I'm doing now, using an incredibly stupid example:
Private Sub PrintNumbers(includeEvenNumbers As Boolean, includeOddNumbers As Boolean)
For number As Integer = 0 To 9
Select Case number
Case 0, 2, 4, 6, 8
If includeEvenNumbers Then
Console.WriteLine(number)
End If
Case 1, 3, 5, 7, 9
If includeOddNumbers Then
Console.WriteLine(number)
End If
End Select
Next
End Sub
Sometimes I'll even write my cases inside out:
Private Sub PrintNumbers(includeEvenNumbers As Boolean, includeOddNumbers As Boolean)
For number As Integer = 0 To 9
Select Case True
Case includeEvenNumbers
If number Mod 2 = 0 Then
Console.WriteLine(number)
End If
Case includeOddNumbers
If number Mod 2 <> 0 Then
Console.WriteLine(number)
End If
End Select
Next
End Sub
What I'd really like to do instead:
Private Sub PrintNumbers(includeEvenNumbers As Boolean, includeOddNumbers As Boolean)
For number As Integer = 0 To 9
Select Case number
Case 0, 2, 4, 6, 8 When includeEvenNumbers
Console.WriteLine(number)
Case 1, 3, 5, 7, 9 When includeOddNumbers
Console.WriteLine(number)
End Select
Next
End Sub
Notice that I used the When
keyword, which is currently only used in Try...Catch
blocks.
Can this be done? Who do I talk to to make this happen?
EDIT (1/2)
What's important is that this code would first evaluate When <expression>
. Only if that evaluates to True
, it would go on to evaluate the Case <expression>
.
The main reason why I want to do this is because I'd like to write cases where the test condition throws an exception if the circumstances are right (or wrong, depending on how you look at it). I'd like to skip those cases if the precondition is true.
EDIT (2/2)
It's pretty clear by now that what I'm asking for is not possible in the current iteration of VB. So I searched for a place to submit feature requests to the .NET development team, then found out they have a uservoice platform.
Long story short: if you'd like to see this implemented, tap the vote button on this page: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/4274712-add-when-keyword-support-to-select-case-stat