all groups > dotnet clr > april 2006 >
You're in the

dotnet clr

group:

VB.Net evaluates 'if' conditions unnecessarily?


VB.Net evaluates 'if' conditions unnecessarily? Tim_Mac
4/25/2006 10:23:38 AM
dotnet clr:
hi,
in c# i can run the following code without risk of
NullReferenceException:

if(textNode != null && !String.IsNullOrEmptytextNode.InnerText))

the reason it is safe to run is because if the first condition is
false, then the CLR will not bother to examine further conditions
(false and anything else is still false...) and the second part of the
condition will not evaluate.

however, in VB, i am running the following code:

If Not textNode Is Nothing And Not
String.IsNullOrEmpty(textNode.InnerText) Then ...

and i get a NullReferenceException... what gives?
thanks for any tips.
tim
Re: VB.Net evaluates 'if' conditions unnecessarily? Tim_Mac
4/25/2006 2:43:19 PM
hi jon,
that must be it. many thanks for adding this new word to my vb
vocabulary. it makes you wonder what the normal AND is supposed to do
then...
AndNotAlsoButOnlyIfTheFirstOneIsNotFalse.

i'm sure the docs have a perfectly logical answer. no need to explain.

thanks again
tim
Re: VB.Net evaluates 'if' conditions unnecessarily? Jon Skeet [C# MVP]
4/25/2006 6:27:23 PM
[quoted text, click to view]

I believe you want the AndAlso operator. The equivalent for "or" is
OrElse.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: VB.Net evaluates 'if' conditions unnecessarily? Scott M.
4/25/2006 7:46:03 PM
"And" evaluates both sides of the conditional regardless of the first
conditional actual value. Same is true for "Or". This is for situations
when the second conditional is a method call and you want the method to run
regardless of its return value.

"AndAlso" and "OrElse" were added to solve that issue.



[quoted text, click to view]

Re: VB.Net evaluates 'if' conditions unnecessarily? Jon Skeet [C# MVP]
4/26/2006 12:00:00 AM
[quoted text, click to view]

I would guess it's historical - because And and Or historically didn't
do short-circuiting, the behaviour wasn't changed for VB.NET. I doubt
that many people really *want* the non-short-circuiting behaviour to be
the more common one...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: VB.Net evaluates 'if' conditions unnecessarily? Greg Young [MVP]
4/26/2006 1:38:13 AM
I agree with this completely .. I remember the first time I came accross
this years ago having not come a VB background I found it to be the oddest
thing.

The "interesting" things we bring forward for the sake of "backwards
thought" compatibility :)


[quoted text, click to view]

Re: VB.Net evaluates 'if' conditions unnecessarily? Scott M.
4/27/2006 6:44:20 PM
Actually, MS did change the non-short circuited functionality of "And" and
"Or" to be short-circuited in the first .NET beta.

Too many existing VB 6.0 developers that had already known about the
original behavior complained that switching the operation of "And" and "Or"
for .NET would make VB 6.0 code harder to port to .NET, so MS switched them
back to non-short circuited and introduced "AndAlso" and "OrElse" as a way
to have the short-circuited approach available.



[quoted text, click to view]

Re: VB.Net evaluates 'if' conditions unnecessarily? Ben Voigt
5/2/2006 6:35:31 PM
[quoted text, click to view]

C/C++/C# && ..... VB.NET AndAlso (logical) short-circuits
C/C++/C# & ...... VB/VB.NET And (bitwise) always evaluates both operands

[quoted text, click to view]

AddThis Social Bookmark Button