有关以下if语句在VB.NET,这将是在其中的条件将被评估的序列?

<强>例1:

If ( condition1 AND condition2 AND condition3 )
.
.
End If

<强>情况2:

If ( condition1 OR condition2 OR condition3 )
.
.
End If

<强>情况3:

If ( condition1 OR condition2 AND condition3  OR condition4)
.
.
End If
有帮助吗?

解决方案

VB.NET是从C程序员的观点来看很奇怪的兽。作为Gerrie在不同的答复中提到,所有这三个条件在充分评估整体非短路。 AndAlso和OrElse运算可以节约你的时间,如果这就是你想要的。

至于最后的如果,评价的顺序如下:

If ((condition1 OR (condition2 AND condition3))  OR condition4)

作为一个经验法则:如果有任何的ambiguitiy,用括号指定计算顺序明确

其他提示

VB.Net评估所有条件,所以为了在这里并不重要。

如果你想使用短路,使用AndAlso和OrElse运算的关键字。

这是不完全应答手头上的问题,因为它已经回答了,我觉得有必要扩大对“OrElse运算”关键字,由Anton提到的,以及它是相对的:“AndAlso”,因为有人登陆这个问题我其实想对这些解释。

“OrElse运算”和“AndAlso”如果你需要明令评价是非常有用的。

不像“或”,“与”,都表达进行评估,如果语句可以跳过剩下的表达式,如果第一个表达式为需要的值,当你利用“OrElse运算”或“AndAlso”的。

在下面的情况下,表达式的顺序也适用于“OrElse运算”从左到右,但也并不总是评估取决于先前值的结果的所有表达式:

if( Expression1 orelse Expression2 orelse Expression3 )

    ' Code...

End If

如果表达式1为真,则表达式2和3将被忽略。 如果表达式1如果为假,则表达式2被评估,然后表达式3如果表达式2计算结果为True。

If (False OrElse True OrElse False ) then
    ' Expression 1 and 2 are evaluated, Expression 3 is ignored.
End If

If (True OrElse True OrElse True ) then
    ' only Expression 1 is evaluated.
End If

If (True OrElse True OrElse True ) then
    ' only Expression 1 is evaluated.
End If

If (False OrElse False OrElse True ) then
    ' All three expressions are evaluated
End If

的 'OrElse运算' 的使用的替代示例:

If( (Object1 is Nothing) OrElse (Object2 is Nothing) OrElse (Object3 is Nothing) ) then 


    ' At least one of the 3 objects is not null, but we dont know which one.
    ' It is possible that all three objects evaluated to null.
    ' If the first object is null, the remaining objects are not evaluated.
    ' if Object1 is not  NULL and Object2 is null then Object3 is not evaluated

Else

   ' None of the objects evaluated to null.

Endif

在上面的例子中是相同的,如下所示:

If(Object1 Is Nothing)

    ' Object 1 is Nothing
    Return True

Else 
    If(Object2 Is Nothing)
        ' Object 2 is Nothing 
        Return True
    Else
        If(Object3 Is Nothing)
            ' Object 3 is Nothing 
            Return True
        Else
            ' One of the objects evaluate to null
            Return False
        End If    
    End If      
End If 

还有的 AndALso 关键字:

' If the first expression evaluates to false, the remaining two expressions are ignored
If( Expression1 AndAlso Expression2 AndAlso Expression3 ) Then ...

哪些是可用的这样的:

If( (Not MyObject is Nothing) AndAlso MyObject.Enabled ) Then ...

   ' The above will not evaluate the 'MyObject.Enabled' if 'MyObject is null (Nothing)'
   ' MyObject.Enabled will ONLY be evaluated if MyObject is not Null.

   ' If we are here, the object is NOT null, and it's Enabled property evaluated to true

Else

  ' If we are here, it is because either the object is null, or it's enabled property evaluated to false;

End If 

不同于 '与',像“OR'-将始终评估所有表达式:

If( (Not MyObject is Nothing) And MyObject.Enabled ) Then ...

   ' MyObject.Enabled will ALWAYS be evaluated, even if MyObject is NULL,
   ' ---  which will cause an Exception to be thrown if MyObject is Null.

End If 

但是,由于为了能够具有的效果与“OrElse运算”和“AndAlso”,评估天气一个对象为空,应先进行,如在上述实例。

下面将引起一个异常,如果“为MyObject”为空

Try 
    If( MyObject.Enabled  AndAlso (Not MyObject is Nothing) ) Then ...

       ' This means that first expression MyObject.Enabled Was evaluated  to True, 
       ' Second Expression also Evaluated to True

    Else

       ' This means MyObject.Enabled evaluated to False, thus also meaning the object is not null.
       ' Second Expression "Not MyObject is Nothing" was not evaluated.

    End If 
Catch(e as Exception)

    ' An exception was caused because we attempted to evaluate MyObject.Enabled while MyObject is Nothing, before evaluating Null check against the object.

End Try

尽请评论,如果我犯了一个错误或错字在这里,因为我早上写了这个在上午05点16分后2天没有睡觉了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top