Tuesday, November 08, 2005

OrElse, AndAlso in Visual Basic (VB)

OrElse operator in Visual Basic (VB) is used to short-circuit the evaluation in the if condition. Apparently, in VB the Or operator does not short-circuit the evaluation of the second logical condition if the first condition evaluates to a Boolean true.

AndAlso operator is Visual Basic (VB) is used to short-circuit the evaluation in the if condition. The regular AND operator does not short-circuit the evaluation of the second logical condition if the first condition evaluates to a Boolean false.

I believe, OrElse and AndAlso should always be used in VB instead of regular Or and And we are familiar with. This should improve the performance especially the second logical condition requires a function call or database access to evaluate.

Bottom line:
  • There is no "real" differnce between OrElse and Or in the VB. Both produces the same result when used in place of each other. The OrElse operator should perform better than Or operator.
  • There is no "real" difference between AndAlso and And in the VB. Both produces the same result when used in place of each other. The AndAlso operator should perform better than the regular And operator.

No comments:

Post a Comment