In VB.NET
, how can I achieve the following logic?
Select Case testString
Case Contains("ABC") : Debug.Print "ABC"
Case Contains("XYZ") : Debug.Print "XYZ"
Case Else : Debug.Print "Other"
End Select
In VB.NET
, how can I achieve the following logic?
Select Case testString
Case Contains("ABC") : Debug.Print "ABC"
Case Contains("XYZ") : Debug.Print "XYZ"
Case Else : Debug.Print "Other"
End Select
Simply:
If testString.Contains("ABC") Then
Debug.Print "ABC"
ElseIf testString.Contains("XYZ") Then
Debug.Print "XYZ"
Else
Debug.Print "Other"
End If
You cannot check this kind of function results in a Select Case statement, so using a simple If statement is your best and easier choice.
You can always do this:
Select Case True
Case testString.Contains("ABC") : Debug.Print("ABC")
Case testString.Contains("XYZ") : Debug.Print("XYZ")
Case Else : Debug.Print("Other")
End Select
But it would work only in very concrete cases and is not very clear and necessary at all.
In VB.NET you could use this Select
:
Select Case True
Case testString.Contains("ABC")
Debug.Print("ABC")
Case testString.Contains("XYZ")
Debug.Print("XYZ")
Case Else : Debug.Print("Other")
End Select
However, in this case i would prefer a simple If
clause.
If testString.Contains("ABC") Then
Debug.Print("ABC")
ElseIf testString.Contains("XYZ") Then
debug.Print("XYZ")
Else
Debug.Print("Other")
End If
This would be clearer and also compatible with C#. switch
allows only constant expressions.