When to use AndAlso vs And ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Greetings,

If x = y And m = n The
....
End If

If x = y AndAlso m = n Then
....
End If

When do I need to use AndAlso vs And?

Thanks,
Rich
 
For the sample you provided, you are doing a strictly logical operation. Use
the "AndAlso" since it's more efficient (it doesn't evaluate the second
operand if the first is false).
The "And" operator is usually just used for bitwise operations. You can
also use "And" for logical operations, but it evaluates both operands always.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
If you use "And", then it processes all of the clauses and then decides
if it's true or not.
So it checks both (x=y) and (m=n).

If you use "AndAlso", it stops processing when it hits a condition that
is false. So if (x=y)
is false, it never checks (m=n). This is more efficient because it
doesn't do
unnecessary processing, and also can be handy, if you want to do
something like this:

If (rdr IsNot Nothing) AndAlso rdr.Rows(0).Items(0).ToString = "abc" ...

Unlike VB6, it won't crap out if rdr is nothing, because it won't
process the
second clause.

For the "or" operator, they added "OrElse" which does the same thing.
It's called short-circuiting.

I believe this is the way "and" and "or" work in C/C++/C#. I think they
would
have just changed "and" and "or" in VB, but it would have freaked out
too many
people and required more work when converting from VB6, so they just
added
new constructs.

Robin S.
(more info than you wanted, wasn't it?)
 
what a load of crap it worked well enough in VB6

unnecessary change isn't good.
it's unnecessary

-Aaron
 
what a load of crap it worked well enough in VB6

Really? How would VB6 handle this code if the instance were null?

If (Not obj Is Nothing) And (obj.Property = "something") Then
'Do something here
End If

It would have crashed because IIRC, And was not short circuited in VB6
.. Using AndAlso avoids this in VB.Net.
unnecessary change isn't good.
it's unnecessary

The original VB.Net beta proposed adding two new operators called
BitAnd and BitOr which would be used for bitwise operations and
changing And and Or to only be logical operations. That would have
been an unnecessary change and, thankfully, they changed it back.
 
Rich said:
When do I need to use AndAlso vs And?

RobinS explained how those keywords work so I won't repeat that
information. My recommendation is to ALWAYS use AndAlso and OrElse
when doing logical comparisons. It costs you nothing to use them and
you get the benefits of short circuiting.

Use And and Or for bitwise operations.

The exception being if you are reusing code from VB6 and it might not
be worth the time to change the And's and Or's to AndAlso and OrElse.

Chris
 
and you seriously can't find a workaround for that?

I'd wrap it in a function and be done with it. lol
shit can you seriously not figure out the logic here?

Public Function DoSomething(obj as object) as boolean
on error resume next

If obj.property <> "something"
DoSomething = False
Else
DoSomething = True
end select

End function
 
you still don't understand the point.

unnecessary change isn't good.

it is unnecessary

-Aaron
 
While I concur with the 'use AndAlso and OrElse when doing logical
comparisons' rule of thumb, as usual there are always exceptions to the
rule.

One such exception is the situation where one has 2 functions where both
return a boolean, both execute some code that is critical to the application
and the result of both must be True for some other action to be taken, e.g.:

Private Sub Form_Load(...) Handles MyBase.Load

If FunctionA() And FunctionB() Then
' Take some critical action before continuing
End If

End Sub

Private Function FunctionA() As Boolean

Try
' Some critical code here
Return True
Catch
Return False
End Try

End Function

Private Function FunctionB() As Boolean

Try
' Some other critical code here
Return True
Catch
Return False
End Try

End Function

If AndAlso was used in this case, and FunctionA returned False then
FunctionB would not be called and a critical step would be 'missed'.

I am not advocation that this is a good way to go about things, merely that
it demonstrates a situation where 'short-circuiting' is not desirable.
 
what a load of crap it worked well enough in VB6

unnecessary change isn't good.
it's unnecessary
 
you still don't understand the point.

unnecessary change isn't good.

it is unnecessary

I do understand the point, and I agree that unnecessary change isn't
good.

And and Or STILL function like they did before!!

Nothing has changed, a feature has been ADDED.

Chris
 
what a load of crap it worked well enough in VB6

unnecessary change isn't good.
it's unnecessary

Ok so maybe change just for the sake of change is sometimes a Bad
Thing, but I don't see a problem with having short-ciruit logic
operators. It's not as if they are the ONLY logic operator, you can
still use "And" and "Or".
 
it's not a feature.. it's a point of confusion

VB.net has NO TANGIBLE BENEFITS over VB6.
the IDE sucks; it is slower it is more difficult; it doesn't produce
portable code.

I used to be able to write VB6 code, cut and paste it into Notepad and
run it as a VBS file.

When VB.net comes 90% close to being able to have this sort of
simplicity- is when I lower myself to using VB 2005.

and the point of the matter is that it's not called VB.net 2005; it is
merely called VB 2005.

Why are we posting in a newsgroup named DOTNET when we're not talking
about a product named DOTNET?

why are there 40 dead newsgroups at microsoft.public.vb?

why did they bother changing the newsgroup name?

-Aaron
 
But Chris,

If you use logic in this way, he can't continue to rail against the machine,
tossing his little tantrums because he needs to change his way of thinking
and coding

Bruce
 
I don't need to change my way of coding.

I'll stay happy, wellpaid in the database world and say screw
programming

-Aaron
 
it's not a feature.. it's a point of confusion

VB.net has NO TANGIBLE BENEFITS over VB6.
the IDE sucks; it is slower it is more difficult; it doesn't produce
portable code.

I used to be able to write VB6 code, cut and paste it into Notepad and
run it as a VBS file.

When VB.net comes 90% close to being able to have this sort of
simplicity- is when I lower myself to using VB 2005.

and the point of the matter is that it's not called VB.net 2005; it is
merely called VB 2005.

Why are we posting in a newsgroup named DOTNET when we're not talking
about a product named DOTNET?

why are there 40 dead newsgroups at microsoft.public.vb?

why did they bother changing the newsgroup name?

-Aaron

VB.NET allows more use of operating system functions than VB6.
A pitfall in VBS is that malicious people use it to wreak havoc on your
system.
Because of this, a lot of OS's prompt you before they run the script.
This gets pretty annoying sometimes.
Also, there are more IDE's out there than just the one Microsoft has
created.
there is a good one at icsharpcode.net It is called SharpDevelop. It
was written entirely of C#.NET code.
It can be used to write VBNET C++NET & C#NET.

mediocrevbprogrammer.freed
 
why do i need to?

shit works fine already

why fix what isn't broken?
their IDE is too slow, execution is too slow.

introducing new functionality isn't required.
quicker, easier deployments are required.

it would have been really nice to be able to make standalone EXE files
with VB 2005.
I was really hoping that they were going to stop the madness.

For starters, there isnt' even a single standard way for me to
determine which version of the framework is registered on the current
version of windows.

real quick-- my 7 machines; how do i determine which versions of the
framework I _PROBABLY_ have installed on these 7 machines? keep in
mind that it is 3 different operating systems :)

and a few different service packs.

GAME02 - WIndows XP SP2
GAME03 - Windows XP SP2
DC1 - Dualie Athlon Server 2003 SP0
PDC - Quad Pentium Pro 333mhz, Server 2000 SP4
FILE01 - Windows 2000 Professional Barebone SP4
FILE02 - Vista SP0
SQL1 - Athlon 1333 Server 2003 SP0
SQL8 - Quad Windows NT Server SP6
IIS01 - Pentium 2 400 mhz Server 2003 Web Edition SP1
IIS02 - Dualie 733 mhz Server 2000 SP4

I probably have 20 different IDE throughout these

Seriously.... please describe to me.. how I can determine 'which
version of the framework is on which machine' because I
guaran-friggin-tee it that your solution will be incomplete and _wrong_

Yeah-- if every WIndows machine in the world had the .NET framework;
life would be simple.

But working for a large corporation; asking them to install the .NET
framework on an old Server 2000 machine? A Server that's been stable
for 4 years without problems?

You think that they will just gamble on letting some rogue Windows
Administrator install the framework?

Yes; if Microsoft could guarantee that the 3.0 framework will work
FLAWLESSLY with all previous versions-- then everything would be all
fine and good if they would just push 3.0 framework onto everything
from Windows 95 to NT4 to Windows 2000, 2003, XP and VIsta?

But am I really ready to take yet another FORCED UPGRADE WITHOUT REAL
TANGIBLE ROI BENEFITS?

There has _NEVER_ been a single .NET development project with a
positive ROI

complete and utter failures

VB6 development is faster, deployment is easier.

-Aaron
 
What about AndNotAlsoProvided?

George

Chris said:
RobinS explained how those keywords work so I won't repeat that
information. My recommendation is to ALWAYS use AndAlso and OrElse
when doing logical comparisons. It costs you nothing to use them and
you get the benefits of short circuiting.

Use And and Or for bitwise operations.

The exception being if you are reusing code from VB6 and it might not
be worth the time to change the And's and Or's to AndAlso and OrElse.

Chris
 
Back
Top