wally_davis
asked on
Determining the status of an object using Or conditional operator
I've created the following objects in a VBScript:
Set oLocator = CreateObject("WbemScriptin g.SWbemLoc ator")
Set oService = oLocator.ConnectServer(sSe rver, "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
Set oLocator = CreateObject("WbemScriptin
Set oService = oLocator.ConnectServer(sSe
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
ASKER
<sigh>, no that didn't work.
ASKER
I've tried just about every parenthesis position and still it doesn't work. hmmm
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Been so long I can't remember.
Does this work?
If (not isempty(oreg)) or (not oreg is nothing) then
'Code
Else
'Code
End if