Link to home
Start Free TrialLog in
Avatar of wally_davis
wally_davisFlag for United States of America

asked on

Determining the status of an object using Or conditional operator

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
else
     // 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,
Wallace
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

When combining boolean statements like that, be careful with parentheses.

Does this work?

If (not isempty(oreg)) or (not oreg is nothing) then
'Code
Else
'Code
End if
Avatar of wally_davis

ASKER

<sigh>, no that didn't work.
I've tried just about every parenthesis position and still it doesn't work. hmmm
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:
IsNull
IsEmpty
Is Nothing
etc.
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.
ASKER CERTIFIED SOLUTION
Avatar of Darren Collins
Darren Collins
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Been so long I can't remember.