Short circuiting by using AndAlso and OrElse

  • Thread starter Thread starter Euvin
  • Start date Start date
E

Euvin

I am kind of confuse as to how these operators work:
AndAlso and OrElse

Anyone care to explain. Some examples would be great!
 
A popular use of AndAlso would be something like this:

if ((anObj isnot Nothing) AndAlso (anObj.TheProperty = False)) then
' do some work
endif

With an ordinary "And" in the above, you would get an "Object not set..."
error in the second clause. The new syntax looks at the furst part, and
skips out when the object is not set.

OrElse is processed similarly. If the first part is true, then there is no
evaluation of the second part.
 
Euvin,

The AndAlso and the OrElse are the same as in every orther language the And
and the Or.

A strong lobby from the VB6 side made that in VB this was choosen those two
above instead of the Or and the And.
Despite that less important things were changed with the introduction of
VB2001 this was a main option for the VB6 side.

The reason was that probably in 0,01% of all programs the OR and AND are in
VB as well used for boolean operations.

Something as AndBool and OrBool they called a breaking change.

The behaviour of And And Or in VB is too a little bit else because of this,
because everything is evaluated first.

Cor
 
Here the explanation of someone from the VB dev team ( Paul Vick software
architect of the VB Development team )

http://www.panopticoncentral.net/articles/919.aspx

and you wil see that

And & Or are used for bitwise comparisation

AndAlso & OrElse are used for logical comparisation

despite what people say or think it is clear that they took this idea from C
" like C where there are separate logical operators (|| &&) and bitwise
operators (| &)."

HTH

Michel
 
Euvin said:
I am kind of confuse as to how these operators work:
AndAlso and OrElse


Place the caret on 'AndAlso' or 'OrElse' and press the F1 key in the IDE's
text editor.
 
Michel,

C is using || and &&.
The the shortcutted Or and And are much older: it was already in the first
versions of Cobol.

:-)

Cor
 
it was already in the first versions of Cobol.

Yes i know that we have just rewritten a production system from Cobol so i
have seen some familiair constructs in these sources , as we had to rewite
the system from printed out Cobol source

However my point is that from origin most MS developers are C proggers and
if you read most MS DEV blogs you read that they took ideas from there
background "C" and introduced it to VB

Michel
 
Back
Top