Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « IF and IIF in VB.NetVisual Studio 2010 and .NET Framework 4 Training Kit June 2010 Release Available »
    comments

    I still see too many people use And and Or when beginning with VB.Net.

    Since the introduction of VB.Net, way back in 2002, there have been AndAlso and OrElse. In most cases you should prefer them over And and Or.

    Now why would they have picked a less logical name for something you should actually use more? As always it is simple, backward comparability. And it goes back to the beginning of the B in VB. The keywords and their behavior has been in there since pretty much the beginning. So why change it? It would seem logical now to change it, but it wasn't way back in 2002.

    You can read the exact reasoning from Paul Vick himself.

    When working with reference types you should probably always use AndAlso. Because it is more of a no-brainer than And. Just look at this:

    1. If person IsNot Nothing And person.Name.Equals("Baes") Then
    2. ...

    The above code will throw an exception when person is Nothing. Because both sides of the And will be evaluated no matter what. To avoid this you could write:

    1. If person IsNot Nothing Then
    2. If person.Name.Equals("Baes") Then
    3. ...

    But that seems wrong.

    Or you could write.

    1. If person IsNot Nothing AndAlso person.Name.Equals("Baes") Then
    2. ...

    And not get an exception because AndAlso will only execute the first part, see that it doesn't comply with the rules and will no longer evaluate the second part.

    It all seems simple enough and it is simple enough. I just see that people keep forgetting it.

    BTW: Paul Vick is now on the T-SQL team, and we know there is lots to improve there ;-).

    About the Author

    User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
    Social SitingsTwitterLinkedInHomePageLTD RSS Feed
    andalso, vb.net
    Instapaper

    2 comments

    Comment from: chaospandion [Member] Email
    chaospandion This is some good advice to follow. When looking at C# you almost always see && and || which are equivalent to AndAlso and OrElse.
    06/22/10 @ 16:30
    Comment from: traingamer [Member] Email
    traingamer Good article. As an old school VB developer, I wasn't even aware of AndAlso or OrElse.
    06/23/10 @ 08:37

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)