I'm trying to get a lazy evaluation with 'And' in my Excel macro by doing the following:
If Not myObject Is Nothing *And* myObject.test() Then
'do something'
Else
'do something else'
End If
I know lazy evaluation exists in VB.NET as AndAlso
and OrElse
but cannot find anything similar in VBA. If lazy evaluation does not exist in VBA, what's the best way to structure the code so that it will evaluate the way I expect?
The OrElse operator is defined only for the Boolean Data Type. Visual Basic converts each operand as necessary to Boolean before evaluating the expression. If you assign the result to a numeric type, Visual Basic converts it from Boolean to that type such that False becomes 0 and True becomes -1 .
A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression.
OrElse is a short-circuiting operator, Or is not. By the definition of the boolean 'or' operator, if the first term is True then the whole is definitely true - so we don't need to evaluate the second term. Or doesn't know this, and will always attempt to evaluate both terms.
VB has operators AndAlso and OrElse, that perform short-circuiting logical conjunction. Why is this not the default behavior of And and Or expressions since short-circuiting is useful in every case.
The only short circuiting (of a sort) is within Case
expression evaluation, so the following ungainly statement does what I think you're asking;
Select Case True
Case (myObject Is Nothing), Not myObject.test()
MsgBox "no instance or test == false"
Case Else
MsgBox "got instance & test == true"
End Select
End Sub
This is an old question, but this issue is still alive and well. One workaround I've used:
Dim success As Boolean ' False by default.
If myObj Is Nothing Then ' Object is nothing, success = False already, do nothing.
ElseIf Not myObj.test() Then ' Test failed, success = False already, do nothing.
Else: success = True ' Object is not nothing and test passed.
End If
If success Then
' Do stuff...
Else
' Do other stuff...
End If
This basically inverts the logic in the original question, but you get the same result. I think it's a cleaner solution than the others here that only use If
statements. The solution using a Select
statement is clever, but if you want an alternative using only If
statements, I think this is the one to use.
Or you could create a function that takes your object as a parameter and returns boolean for either case. That's what I usually to.
i.e.
if Proceed(objMyAwesomeObject) then
'do some really neat stuff here
else
'do something else, eh
end if
...
end sub
private function Proceed(objMyAwesomeObject as Object)
if not objMyAweseomeObject is nothing then
Proceed = true
elseif objMyAwesomeObject.SomeProperty = SomeValue then
Proceed = true
else
Proceed = false
endif
end function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With