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 / Windows Forms / WinForm General / January 2008

Tip: Looking for answers? Try searching our database.

Order of evaluation in an If statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Victory - 05 Jan 2008 05:35 GMT
Hi,
I am running into a problem which is best described with an example:

Dim aList as ArrayList = Nothing
..
if (aList IsNot Nothing) And (aList.Count > 0) then
...

This always gives me an exception. I have also tried switching the order
like:

if (aList.Count > 0) And (aList IsNot Nothing) then
..
and i get the same thing. I have had to break this into separate if
statements. Can anyone shed a light on this and explain why this does
not work?
thanks,
Mars
Jack Jackson - 05 Jan 2008 06:48 GMT
>Hi,
>I am running into a problem which is best described with an example:
[quoted text clipped - 14 lines]
>thanks,
>Mars

If (aList IsNot Nothing) AndAlso (aList.Count > 0) Then

Use AndAlso instead of And.  And and Or evaluate all parts of the
expression, AndAlso and OrElse stop evaluating when the final result
is known.  Use And and Or only for logical operations on numeric
values.

Be aware that Iif works the same way - all arguments are always
evaluated.
Stephany Young - 05 Jan 2008 07:46 GMT
Close!

AndAlso stops evaluating when the expression(s) to the left evaluates to
False.

OrElse stops evaluating when the expression(s) to the left evaluates to
True.

>>Hi,
>>I am running into a problem which is best described with an example:
[quoted text clipped - 24 lines]
> Be aware that Iif works the same way - all arguments are always
> evaluated.
Victory - 05 Jan 2008 08:10 GMT
I am surprised. And works this way in C#, C and C++ so what required
this change in VB .NET?
Thank you both!
Mars.
Stephany Young - 05 Jan 2008 10:34 GMT
Simple answer = VB was never based or derived from C/C++, it was based on
BASIC.

The AndAlso and OrElse operators were added in VB.Net 2003 to provide
exactly that 'short-circuiting' behaviour.

>I am surprised. And works this way in C#, C and C++ so what required
> this change in VB .NET?
> Thank you both!
> Mars.
>
> *** Sent via Developersdex http://www.developersdex.com ***
Herfried K. Wagner [MVP] - 05 Jan 2008 13:44 GMT
"Victory" <csharp@devdex.com> schrieb:
>I am surprised. And works this way in C#, C and C++ so what required
> this change in VB .NET?

Actually there is no operator named 'and' in C#, C, and C++, respectively.
However, C# has both '&' and '&&' operators, which share semantics with VB's
'And' and 'AndAlso' operators.  '|' maps to 'Or' and '||' maps to 'OrElse'.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Victory - 05 Jan 2008 14:29 GMT

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.