15

有没有办法像这样转换一些代码:

If someString <> "02" And someString <> "03" And someString <> "06" And someString <> "07" Then
     btnButton.Enabled = False
End If

有点像这样(一个变量的多个值)

If someString <> "02", "03", "06", "07" Then
     btnButton.Enabled = False
End If
4

4 回答 4

26

Contains工作吗?

Dim testAgainst As String() = {"02","03","06","07"}
If Not testAgainst.Contains(someString) Then
    btnButton.Enabled = False
End If
于 2013-05-23T22:44:37.830 回答
14

Select在简单的情况下,您可以(ab)使用它:

Select Case someString
    Case "02", "03", "06", "07"
    Case Else
        btnButton.Enabled = False
End Select

另外,我使用的一个常见扩展是:

<Extension()>
Function [In](Of TItem, TColl)(this As TItem, ParamArray items() As TColl)
    Return Array.IndexOf(items, this) > -1
End Function

所以:

If Not someString.In("02", "03", "06", "07") Then
    btnButton.Enabled = False
End If
于 2013-05-23T22:45:16.530 回答
5
Dim invalidvalues As New List(Of String) From { _
    "02", _
    "03,", _
    "04", _
    "07" _
}

If invalidvalues.Contains(x) Then
    btnButton.Enabled = False
End If
于 2013-05-23T22:46:15.417 回答
0

这个怎么样?

Imports System.Text.RegularExpressions    

btnButton.Enabled = Regex.IsMatch(someString, "^0[2367]$")
于 2019-06-11T14:29:55.917 回答