|
-
Jun 29th, 2005, 09:34 PM
#1
And/Or vs AndAlso/OrElse
Here I am again, although with a less interesting topic this time.
I've noticed that a great many people seem to use And and Or by default, rather than AndAlso and OrElse. In most cases it doesn't really matter, but I think it is because many people don't understand the difference, rather than by design or even laziness. Personally, I never use And or Or unless I specifically need to, i.e. I use AndAlso and OrElse by default.
Although it will generally make only a microscopic difference, it is more efficient to use AndAlso and OrElse because they use short-circuiting, meaning that they only evaluate the second condition if it is needed. I would generally only use And and Or if the second expression was a function that needed to be executed whether the first expression was true or not.
As I said, in the majority of cases there will be no noticeable difference. I mention it because if beginners get in the habit of using And and Or by default then there will be times that they get errors that they do not understand. For those who aren't sure, take the following example where you only want to create a particular form if it doesn't already exist.
Code:
Dim myChild As ChildForm
Private Sub ShowChild()
If Me.myChild Is Nothing OrElse Me.myChild.IsDisposed Then
Me.myChild = New ChildForm
Me.myChild.Show()
Else
Me.myChild.Activate()
End If
End Sub
Code:
Dim myChild As ChildForm
Private Sub ShowChild()
If Me.myChild Is Nothing Or Me.myChild.IsDisposed Then
Me.myChild = New ChildForm
Me.myChild.Show()
Else
Me.myChild.Activate()
End If
End Sub
The first example will work correctly, while the second example will throw a NullReferenceException if the form has not been instantiated because it tries to evaluate Me.myChild.IsDisposed on a null reference.
The moral of the story is this: make the extra four key presses and use AndAlso and OrElse by default to avoid the possibility of an unnoticed error. Only use And and Or when you specifically need to.
Last edited by jmcilhinney; Mar 26th, 2011 at 07:46 AM.
-
Jun 29th, 2005, 10:22 PM
#2
Re: And/Or vs AndAlso/OrElse
Most people don't use it because it's an addition to VB first introduced in .NET.... IE, it wasn't available in VB6-.
Tg
-
Jun 29th, 2005, 10:32 PM
#3
Re: And/Or vs AndAlso/OrElse
 Originally Posted by techgnome
Most people don't use it because it's an addition to VB first introduced in .NET.... IE, it wasn't available in VB6-.
Tg
Quite right. I had intended that that be implied by my saying that many don't understand the difference. I guess the truth of the matter is that many actually don't know that the alternative exists. No doubt that is what you were implying. I guess old habits do die hard and it would be difficult to break a habit like that without a compelling reason, even if you were aware of the alternative.
-
Jun 29th, 2005, 11:31 PM
#4
Re: And/Or vs AndAlso/OrElse
I didn't know those tests existed, thanks!
-
Jul 1st, 2005, 04:27 AM
#5
Hyperactive Member
Re: And/Or vs AndAlso/OrElse
AndAlso is great for checking if a object is Nothing before doing a check against a value in the object, it's what I use it for most of the time anyways..
If Not (Obj Is Nothing) AndAlso (Obj.Value = "Whatever") Then
Hinder
-
Jul 1st, 2005, 02:44 PM
#6
Frenzied Member
Re: And/Or vs AndAlso/OrElse
didn't Java actually 'introduce' that concept...of Short Circuiting I mean? It's actually been in OOP for quite some time but .net just exposed it to the Microsoft users...correct me if I'm wrong.
-
Jul 1st, 2005, 03:04 PM
#7
Re: And/Or vs AndAlso/OrElse
No.... C has had it for a long time.... as did Pascal.... and other languages.... short circuiting is nothing new.....
Tg
-
Jul 1st, 2005, 03:05 PM
#8
Fanatic Member
Re: And/Or vs AndAlso/OrElse
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|