Это не совсем ответит на вопрос, так как он уже был дан ответ, я почувствовал необходимость расширять ключевое слово «OrElse», упомянутое Антоном, а также относительно: «AndAlso», потому что кто-то приземлялся по этому вопросу я действительно хочу объяснить это.
«OrElse» и «AndAlso» Полезно, если вам требуется явно упорядоченная оценка.
В отличие от 'Or' и 'And', если бы все выражения оценивались, оператор If мог пропустить оставшиеся выражения, если первое выражение оценивается до желаемого значения, когда вы используете «OrElse» или «AndAlso», ,
В следующем случае порядок выражения применимы к «OrElse» слева направо, но и не всегда оценивать все выражения в зависимости от результата предыдущего значения:
if(Expression1 orelse Expression2 orelse Expression3)
' Code...
End If
Если Выражение1 истинно, тогда выражения 2 и 3 игнорируются. Если выражение 1, если False, тогда выполняется выражение Expression2, а затем выражение Expression3, если выражение Expression2 имеет значение 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», оценивая погоду, объект должен иметь значение null, как и в приведенных выше примерах.
Ниже будет вызывать исключение, если «MyObject» является Null
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
DO, пожалуйста, комментарий, если я сделал ошибку или опечатку здесь, как я писал в 5:16 утра утром после 2-х дней без спать.
'Ass Gerrie упоминается ...' –