Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2006

Tip: Looking for answers? Try searching our database.

VB.Net evaluates 'if' conditions unnecessarily?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim_Mac - 25 Apr 2006 18:23 GMT
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
Jon Skeet [C# MVP] - 25 Apr 2006 18:27 GMT
> in c# i can run the following code without risk of
> NullReferenceException:
[quoted text clipped - 13 lines]
> and i get a NullReferenceException... what gives?
> thanks for any tips.

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Tim_Mac - 25 Apr 2006 22:43 GMT
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
Scott M. - 26 Apr 2006 00:46 GMT
"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.

> hi jon,
> that must be it.  many thanks for adding this new word to my vb
[quoted text clipped - 6 lines]
> thanks again
> tim
Jon Skeet [C# MVP] - 26 Apr 2006 06:03 GMT
> "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.

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...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Greg Young [MVP] - 26 Apr 2006 06:38 GMT
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 :)

>> "And" evaluates both sides of the conditional regardless of the first
>> conditional actual value.  Same is true for "Or".  This is for situations
[quoted text clipped - 8 lines]
> that many people really *want* the non-short-circuiting behaviour to be
> the more common one...
Scott M. - 27 Apr 2006 23:44 GMT
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.

>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
[quoted text clipped - 16 lines]
>> that many people really *want* the non-short-circuiting behaviour to be
>> the more common one...
Ben Voigt - 03 May 2006 00:35 GMT
>> in c# i can run the following code without risk of
>> NullReferenceException:
[quoted text clipped - 16 lines]
> I believe you want the AndAlso operator. The equivalent for "or" is
> OrElse.

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.