tags:

views:

229

answers:

4
+4  Q: 

Or versus OrElse

Whats the difference between or and OrElse?

if temp is dbnull.value or temp = 0

produces the error *Operator '=' is not defined for type 'DBNull' and type 'Integer'.**

while this one works like a charm!

if temp is dbnull.value OrElse temp = 0
A: 

(I've looked at other answers and realized I was terribly wrong)

The OrElse operator "performs short-circuiting logical disjunction on two expressions", that is to say: if the left operand is true and so the entire expression is guaranteed to be true the right operand won't even be evaluated (this is useful in cases like:

string a;
//...
if (a is null) or (a = "Hi") //...

to avoid a NullReferenceException throw by the right-hand operand.

I'm sincerely astonished that this (lazy evaluation) isn't the default behaviour of or and and as it is in C/C++ and C# (and many other languages...)

Utaal
The thing is, in VB classic, there was *just* And and Or, which were non-short-circuiting. I **think** I'm right in saying that the first betas of VB.NET actually changed the behaviour of these operators - there was uproar, so they were changed back and AndAlso and OrElse (short-circuiting) were introduced. I can only imagine the alternative names they must have considered if these were the ones that were the best...
AakashM
+2  A: 

OrElse is short circuited, this means that only one side of the expression will be tested if the first side is a match.

Just like AndAlso will only test one side of the expression if the first half is a fail.

Stevo3000
+7  A: 

OrElse is a short-circuiting operator, Or is not.

By the definition of the boolean 'or' operator, if the first term is True then the whole is definitely true - so we don't need to evaluate the second term.

OrElse knows this, so doesn't try and evaluate temp = 0 once it's established that temp Is DBNull.Value

Or doesn't know this, and will always attempt to evaluate both terms. When temp Is DBNull.Value, it can't be compared to zero, so it falls over.

You should use... well, whichever one makes sense.

AakashM
So Or only makes sense when I call a function after the or that has side effects my code depends on?
Ralph Rickenbach
Or makes sense in all cases where the second item does not trigger error if the first one is true...
awe
@ malach: I suppose so (you really get OrElse behaviour as default in most other languages): It's not a good idea to call functions with side effects in compound conditionals it makes the code unreadable.
Utaal
@ awe: yeah, but why do you even want to waste time evaluating something which by definition won't change the result of the expression?
Utaal
@Utaal, because it's fewer characters to read in the code. Readability is more important than execution speed in most code.
MarkJ
@MarkJ: How is OrElse less readable than Or? I just use OrElse and AndAlso by default. In the places where execution speed does matter (happens often, for exemple conditions checked inside a long loop), you use the good operator without even thinking about it.
Meta-Knight
A: 

This is the same behaviour as with C#, where everyone uses the Coditional Or (||) and the Conditional And (&&), where you also have the normal Or (|) and normal And (&). So comparing C# to VB.Net is:

| => Or

|| => OrElse

& => And

&& => AndAlso

The condifitonal boolean operators are very usefull preventing nested if constructions. But sometimes the normal boolean operators are needed to ensure hitting both code paths.

Bert Heesbeen