【发布时间】:2012-11-22 13:19:04
【问题描述】:
有没有类似的函数或运算符:
If RoleName in ( "Val1", "Val2" ,"Val2" ) Then
'Go
End If
代替:
If RoleName = "Val1" Or RoleName = "Val2" Or RoleName = "Val2" Then
'Go
End If
【问题讨论】:
有没有类似的函数或运算符:
If RoleName in ( "Val1", "Val2" ,"Val2" ) Then
'Go
End If
代替:
If RoleName = "Val1" Or RoleName = "Val2" Or RoleName = "Val2" Then
'Go
End If
【问题讨论】:
尝试使用数组,然后您可以使用 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
【讨论】:
If (Array.IndexOf(New String() {"Val1", "Val2", "Val3"}, RoleName) > -1) Then
Imports System.Linq?
System.Linq 解决了错误'contains' is not a member of 'system.array'。
您可以使用如 LarsTech 所示的 Contains,但添加 In 扩展方法也很容易:
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
【讨论】:
您也可以使用Select..Case 声明:
Select Case RoleName
Case "Val1", "Val2", "Val3"
' Whatever
End Select
【讨论】: