将多个 OR 压缩为一个 If 语句

时间:2021-02-28 15:20:40

标签: vb.net

我想在一个 IF 语句中使用多个 OR 语句,但显然它最终是这样的:

If x = this or x = that or x = these or x = those Then

有没有办法把它压缩成一个更小的语句? 例如

If x = {this, that, those, these}

2 个答案:

答案 0 :(得分:1)

您可以使用 Contains 来确定列表是否包含您的值。假设整数

Dim vals As New List(Of Integer)(New Integer() {this, that, those})

If vals.Contains(x) Then
   ' do something
End If

答案 1 :(得分:0)

人们可以使用 Select Case。作为示例只是一些代码

Dim x As String
x = "this"
Select Case x
    Case "this", "that", "these","those"
        ' Do this that 
    Case "other this", "other that"
        ' do other this that 
    Case Else
End Select

或者您按照建议使用列表here