15

是否有任何函数或运算符,例如:

        If RoleName in ( "Val1",  "Val2" ,"Val2" ) Then
        'Go
    End If

代替:

    If RoleName = "Val1" Or RoleName = "Val2" Or RoleName = "Val2" Then
        'Go
    End If
4

3 回答 3

24

尝试使用数组,然后您可以使用 Contains 扩展:

Dim s() As String = {"Val1", "Val2", "Val3"}
If s.Contains(RoleName) Then
  'Go      
End If

或者没有声明行:

If New String() {"Val1", "Val2", "Val3"}.Contains(RoleName) Then
  'Go
End If

在 OP 中,如果 Contains 扩展不可用,您可以尝试以下操作:

If Array.IndexOf(New String() {"Val1", "Val2", "Val3"}, RoleName) > -1 Then
  'Go
End If
于 2012-11-22T13:22:24.020 回答
16

In您可以使用如 LarsTech 所示的 Contains,但添加扩展方法也很容易:

Public Module Extensions

    <Extension()> _
    Public Function [In](Of T)(value As T, ParamArray collectionValues As T()) As Boolean
        Return collectionValues.Contains(value)
    End Function

End Module

你可以像这样使用它:

If RoleName.In("Val1", "Val2", "Val3") Then
    'Go
End If
于 2012-11-22T14:31:17.030 回答
15

您还可以使用以下Select..Case语句:

Select Case RoleName 
    Case "Val1", "Val2", "Val3"
        ' Whatever
End Select
于 2012-11-22T13:24:21.877 回答