Results 1 to 8 of 8

Thread: And/Or vs AndAlso/OrElse

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: And/Or vs AndAlso/OrElse

    Quote 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.

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: And/Or vs AndAlso/OrElse

    I didn't know those tests existed, thanks!

  5. #5
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    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

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    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.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: And/Or vs AndAlso/OrElse

    Good to know jim thx
    "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
  •  



Click Here to Expand Forum to Full Width