v VB.NET
, ako môžem dosiahnuť nasledujúcu logiku?
Select Case testString
Case Contains("ABC") : Debug.Print "ABC"
Case Contains("XYZ") : Debug.Print "XYZ"
Case Else : Debug.Print "Other"
End Select
odpovede:
4 pre odpoveď č. 1jednoducho:
If testString.Contains("ABC") Then
Debug.Print "ABC"
ElseIf testString.Contains("XYZ") Then
Debug.Print "XYZ"
Else
Debug.Print "Other"
End If
Výsledky tohto druhu funkcie nemôžete skontrolovať vo vyhlásení Select Case, takže pomocou jednoduchého príkazu If je vaša najlepšia a ľahšia voľba.
Vždy to môžete urobiť:
Select Case True
Case testString.Contains("ABC") : Debug.Print("ABC")
Case testString.Contains("XYZ") : Debug.Print("XYZ")
Case Else : Debug.Print("Other")
End Select
Fungovalo by to však iba vo veľmi konkrétnych prípadoch a vôbec nie je jasné a nevyhnutné.
3 pre odpoveď č. 2
Vo VB.NET to môžete použiť 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
V tomto prípade by som však uprednostnil jednoduchý If
klauzula.
If testString.Contains("ABC") Then
Debug.Print("ABC")
ElseIf testString.Contains("XYZ") Then
debug.Print("XYZ")
Else
Debug.Print("Other")
End If
Bolo by to jasnejšie a zlučiteľné aj s C #. switch
umožňuje iba konštantné výrazy.