Determining the status of an object using Or conditional operator

Posted on 2011-05-12
Last Modified: 2012-05-11
I've created the following objects in a VBScript:
            Set oLocator = CreateObject("WbemScripting.SWbemLocator")
            Set oService = oLocator.ConnectServer(sServer, "root\Default", strDom & "\" & strUser, strPass)
            Set oReg = oService.Get("StdRegProv")
Now, I've seen it were the variable is "uninitialized" (empty) or the object doesn't get created and it's equivalent to "Nothing" so I decided to use the Or conditional operator and my findings are a bit confusing. If I write it like this:
If Not (IsEmpty(oReg)) Or Not ((oReg) Is Nothing) Then
     // do this
     // do that
end if
So, when I came across toe oReg object = Nothing it didn't jump down to the else statement as if there oReg object wasn't equal to Nothing. Now, if I put it on separate lines like this it works:
If Not IsEmpty(oReg) Then
   If Not (oReg) Is Nothing
   End If
End If
What's the point of using "Or" if it doesn't work when it should work? I also come across this very problem with using IsNull "Or" IsEmpty as well. It's frustrating.
Maybe one of you experts can clue me in on some syntax I've not discovered  yet. Thanks,
Question by:wally_davis
    LVL 92

    Expert Comment

    by:Patrick Matthews
    When combining boolean statements like that, be careful with parentheses.

    Does this work?

    If (not isempty(oreg)) or (not oreg is nothing) then
    End if

    Author Comment

    <sigh>, no that didn't work.

    Author Comment

    I've tried just about every parenthesis position and still it doesn't work. hmmm

    Author Comment

    I would like to see a really good article written on how one decides to check for data and which one would be a priority over the other using all these different functions:
    Is Nothing
    That is to say, should I always check for an "Empty" uninitialized variable first, then check if it is NULL data and so on. I think Is Nothing "is nothing" more than checking to see if an object was set / instantiated.
    LVL 12

    Accepted Solution

    The If condition will only get to Else if both sides of the 'Or' are False.  If either are True then the first bit before Else is run.

    This sounds obvious, but in your code, the only way for both sides of the 'Or' to be False is if oReg is both Empty and Nothing.  This is impossible.

    When oReg is Nothing, it is also Not Empty, so the If statement is still True and the main condition will run.


    Author Closing Comment

    Been so long I can't remember.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now