views:

4128

answers:

5

In Vb.net, What is difference between And and Andalso. Which should use in coding?

+14  A: 

The 'And' operator evaluate both side, where 'AndAlso' only evaluate the right side if the left side is true. An example:

If mystring IsNot Nothing And mystring.Contains("Foo") Then
  ' bla bla
End If

This throws an exception if mystring = Nothing

If mystring IsNot Nothing AndAlso mystring.Contains("Foo") Then
  ' bla bla
End If

This don't.
So if you come from the C# world, you should use AndAlso which is more like &&.

More info here: http://panopticoncentral.net/articles/919.aspx

Nico
+1  A: 
If Bool1 And Bool2 Then

Evaluates both Bool1 and Bool2

If Bool1 AndAlso Bool2 Then

Evaluates Bool2 if and only if Bool1 is true.

Bryan Anderson
+5  A: 

the And operator will check all conditions in the statement before continuing, whereas the Andalso operator will stop if it knows the condition is false. For example:

if x = 5 And y = 7

Checks if x is equal to 5, and if y is equal to 7, then continues if both are true.

if x = 5 Andalso y = 7

Checks if x is equal to 5. If it's not, it doesn't check if y is 7, because it knows that the condition is false already. (This is called short-circuiting)

Generally people use the short-circuiting method, because it saves on runtime. However, if the second action (in this case y = 7) has a side effect that you want to run whether the first is true or not, i.e.:

if x == 5 And Object.Load()

Then you might want to use And. The reason you might want to use Andalso would be in the case where you want to make sure an object exists before performing an action on it:

if not Object is nothing Andalso Object.Load()

If that used And instead of Andalso, it would still try to Object.Load() even if it were nothing, which would throw an exception.

Ed Marty
If the right side has a side effect you need, just move it to the left side rather than using "And". You only really need "And" if _both_ sides have side effects. And if you have that many side effects going on you're probably doing something else wrong.
Joel Coehoorn
First, Andalso is not primarily used to 'save on runtime', that's preposterous. Second, having the second argument perform some useful 'side effect' is ugly-ass bad practice.
Tor Haugen
You can't argue that it doesn't 'save on runtime' though. And I have found situations where side effects are in fact, not ugly-ass bad practice.
Ed Marty
+1  A: 

Andalso is much like And, except it works like && in C#, C++ etc.

The difference is that if the first clause (the one before Andalso) is true, the second clause is never evaluated - the compound locical expression is "short circuited".

This is sometimes very useful, eg. in an expression such as

If Not IsNull(myObj) Andalso myObj.SomeProperty = 3 Then
   ...
End If

Using the old And in the above expression would throw a NullReferenceException if myObj were null.

Tor Haugen
A: 

Also see this question:
http://stackoverflow.com/questions/55013/should-i-always-use-the-andalso-and-orelse-operators

Also: a comment for those who mentioned using And if the right side of the expression has a side-effect you need:

If the right side has a side effect you need, just move it to the left side rather than using "And". You only really need "And" if both sides have side effects. And if you have that many side effects going on you're probably doing something else wrong. In general, you really should prefer AndAlso.

Joel Coehoorn