473,729 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AndAlso/OrElse usage

Is it better to use AndAlso and OrElse by default rather than the regual And
& Or?

MikeH
Nov 20 '05 #1
10 4632
Cor
Hi Mike,

There is a syntax error in your message
Is it better to use AndAlso and OrElse by default rather than the regual And & Or?


It has to be
Is it better to use AndAlso AndAlso OrElse by default rather than the regual
And AndAlso Or?

This was not serious, but you have my answer,

Cor
Nov 20 '05 #2
* "Mike Hale" <mh***@jcaho.or g> scripsit:
Is it better to use AndAlso and OrElse by default rather than the regual And
& Or?


Did you read the documentation? It depends on the case...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #3
The effects are different.

I exclusively use AndAlso / OrElse because it short circuits the evaluation
of the rest.

And / Or evaluates the entire statement, regardless if a part of it was
false or True. Very inefficient.

AndAlso / OrElse evaluates from left to right until the condition is met.
I think MS would have liked to replace And / Or with AndAlso / OrElse, but
it would have broken many VB applications in the upgrade process. If a
programmer had done something like:

If x and y() then....

and y() did some processing which changed the state of the application...
broken app.
C# and the rest of the languages I can think of evaluate like AndAlso
OrElse.

One other consideration is if you needed to test a value for Null (Nothing)
first and then test a part of that value. And would cause a runtime error:

If not x is nothing and x.myprop...
exception will be thrown if x is nothing because it evaluates to the right.

If not x is nothing andalso x.myprop....

exception will not be thrown if x is nothing.

--

Justin Weinberg
Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com
"Mike Hale" <mh***@jcaho.or g> wrote in message
news:ez******** ******@TK2MSFTN GP11.phx.gbl...
Is it better to use AndAlso and OrElse by default rather than the regual And & Or?

MikeH

Nov 20 '05 #4
Mike,
Most of the time you will be better off using AndAlso & OrElse, however
there are times when you should not.

Remember that AndAlso & OrElse will stop evaluating the expression when the
answer to the test is obvious, which means you can avoid runtime errors and
improve efficiency!

Dim aPerson As Person
If (Not aPerson Is Nothing) AndAlso aPerson.IsAlive then
aPerson.DoSomet hing()
End If

If you attempted the above with just And you will have a runtime error, when
aPerson is Nothing. However with AndAlso the IsAlive is never checked if
aPerson is Nothing.

Hope this helps
Jay

"Mike Hale" <mh***@jcaho.or g> wrote in message
news:ez******** ******@TK2MSFTN GP11.phx.gbl...
Is it better to use AndAlso and OrElse by default rather than the regual And & Or?

MikeH

Nov 20 '05 #5
In article <uz************ **@TK2MSFTNGP09 .phx.gbl>, Justin Weinberg wrote:
The effects are different.


<snip>
I think MS would have liked to replace And / Or with AndAlso / OrElse, but
it would have broken many VB applications in the upgrade process. If a
programmer had done something like:


In the early releases's PDC build and Beta1, this was in fact the case.
MS had made And and Or true logical operators and created BitAnd and
BitOr to handle bit wise evaluation (which is how And and Or work
currently)... Then, when they made several rollback changes (to be more
compatable with VB.CLASSIC) in Beta 2 - they put And and Or back to the
original VB.CLASSIC behavior, removed BitAnd and BitOr and added AndAlso
and OrElse for logical comparisons. Personally, I wished they had never
done the roll backs...

The one I found especially anoying was the reversion to the VB.CLASSIC
way of declaring arrays... With the number in the () beign the last
index in the array rather then the number of elements in the array.

'Current and VB.CLASSIC method
Dim a(5) As Integer ' array with 6 elements 0 - 5

'Beta1 and, IMO, supperior method
Dim a(5) As Integer ' array with 5 elements 0 - 4

Of course, since I don't actually program in VB.NET (I do C#) - I
suppose I can live with it :)
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #6
Boy was that helpful! Thanks! You really are an MVP!

I know that AndAlso/OrElse short circuits, checking from left to right,
which can be more efficient. Perhaps I should have worded my question "What
is more common among the VB developers out there..."

MikeH
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:bo******** *****@ID-208219.news.uni-berlin.de...
* "Mike Hale" <mh***@jcaho.or g> scripsit:
Is it better to use AndAlso and OrElse by default rather than the regual And & Or?


Did you read the documentation? It depends on the case...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>

Nov 20 '05 #7
In article <us************ **@TK2MSFTNGP11 .phx.gbl>, Mike Hale wrote:
Boy was that helpful! Thanks! You really are an MVP!

I know that AndAlso/OrElse short circuits, checking from left to right,
which can be more efficient. Perhaps I should have worded my question "What
is more common among the VB developers out there..."

MikeH


For most things, were your truely doing a logical comparison, I would
use AndAlso or OrElse. But, if you're doing bitwise operations or
comparisons (like checking/setting bit flags) then you use And and Or.

Right tool for the job :)

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #8
"Tom Shelton" <to*@mtogden.co m> wrote in message news:OD******** ******@TK2MSFTN GP10.phx.gbl...
In article <uz************ **@TK2MSFTNGP09 .phx.gbl>, Justin Weinberg wrote: The one I found especially anoying was the reversion to the VB.CLASSIC
way of declaring arrays... With the number in the () beign the last
index in the array rather then the number of elements in the array.

'Current and VB.CLASSIC method
Dim a(5) As Integer ' array with 6 elements 0 - 5

'Beta1 and, IMO, supperior method
Dim a(5) As Integer ' array with 5 elements 0 - 4

Of course, since I don't actually program in VB.NET (I do C#) - I
suppose I can live with it :)

I tend to agree with this too. When you are dynamically build an array, the first element is 0,
so to dimension an array with 1 element you have to use Redim a(0) which has always seemed
confusing to me.

--
Tom Shelton
MVP [Visual Basic]

Nov 20 '05 #9
* "Mike Hale" <mh***@jcaho.or g> scripsit:
Boy was that helpful! Thanks! You really are an MVP!

I know that AndAlso/OrElse short circuits, checking from left to
right,
If you know that, you are on the right way. If you need logical
and/or/... + short circuiting, then use 'AndAlso' or 'OrElse'. In most
other cases use the other operators.
which can be more efficient. Perhaps I should have worded my question "What
is more common among the VB developers out there..."


None is "more common". There is often only one way "right", but that
depends on the situation and the programmer's knowledge about the
_difference_ between those operators.

For me, your questions sounds like this:

"What is "more common"? Using the '+' or the '*' operator."

That's why I wrote the answer before. I am sure you understand what I
want to say.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1793
by: Raterus | last post by:
I kinda just stumbled across these operators, they seem great, as you can forget the second expression depending on the result of the first, but are there any cons to using these? From my viewpoint it seems I can pretty much to a find / replace on my existing code and pick up a performance boost, but something seems fishy that I really never see other people using these! Thanks for any insight! --Michael
11
6484
by: A Traveler | last post by:
I was just curious if anyone knows how the combinations of And/AndAlso and Or/OrElse compare in terms of performance. Which takes more work for the system, performing two evaluations on an And or performing short-circuiting on an AndAlso? Purely for enlightenment. Thanks in advance. - Arthur Dent.
9
10388
by: Lior | last post by:
Hello . I know that the AndAlso and OrElse statements are short-circuiting And and Or statements , respectively . Should I always use (I don't like the word "always" ...) AndAlso instead of And and OrElse instead of Or ?
30
4584
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, If x = y And m = n The .... End If If x = y AndAlso m = n Then .... End If
3
19251
by: Siegfried Heintze | last post by:
Are there operators in C# that are the counterparts of "OrElse" and "AndAlso" that I can use when translating the following program from VB.NET to C#? Thanks, Siegfried Namespace vbtest Module Main Function reflect(x as boolean) as boolean
8
1254
by: Euvin | last post by:
I am kind of confuse as to how these operators work: AndAlso and OrElse Anyone care to explain. Some examples would be great!
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.