并且在VBA中也是/ OrElse

时间:2010-07-14 00:28:34

标签: vb.net vba

我正在尝试通过执行以下操作在Excel宏中使用“和”进行懒惰评估:

If Not myObject Is Nothing *And* myObject.test() Then
    'do something'
Else
    'do something else'
End If

我知道在VB.NET中存在惰性评估AndAlsoOrElse,但在VBA中找不到任何类似的东西。如果在VBA中不存在惰性求值,那么构造代码的最佳方法是什么,以便评估我期望的方式?

8 个答案:

答案 0 :(得分:50)

唯一的短路(一种)是在Case表达式评估中,所以下面的笨拙声明就是我认为你所要求的;

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

答案 1 :(得分:11)

这是一个老问题,但这个问题仍然存在并且很好。我使用过的一种解决方法:

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

这基本上颠倒了原始问题中的逻辑,但是你得到了相同的结果。我认为这是一个比其他人更清晰的解决方案,只使用If语句。使用Select语句的解决方案很聪明,但是如果您想要仅使用If语句的替代方法,我认为这是可以使用的方法。

答案 2 :(得分:3)

或者您可以创建一个函数,将您的对象作为参数,并为两种情况返回布尔值。这就是我通常的目标。

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

答案 3 :(得分:1)

由于以下语法有效

If myObject.test() Then do something

你可以尝试

If Not myObject Is Nothing Then If myObject.test() Then
    'do something'
Else
    'do something else'
End If

当然,如果你想要“做其他事情”,如果myObject Is Nothing,那么这可能不起作用。

答案 4 :(得分:0)

If Not myObject Is Nothing Then
    If myObject.test() Then
        'do something'
    End If
Else
   'do something else'
End If

我认为这就是你必须这样做的方式。

修改

也许就像这样

Dim bTestsFailed as Boolean
bTestsFailed = False

If Not myObject Is Nothing Then
    If myObject.test() Then
        'do something'
    Else
        bTestsFailed = True
    End If
Else
   bTestsFailed = True
End If

If bTestsFailed Then
    'do something else
End If

VBA不是很棒吗?

答案 5 :(得分:0)

this answer上改进了有关同一基本问题的不同问题,这是我选择要做的事情:

dim conditionsValid as boolean

conditionsValid = myObject Is Nothing
if conditionsValid then conditionsValid = myObject.test()
if conditionsValid then conditionsValid = myObject.anotherTest() 

if conditionsValid then
   'do something'
else
   'do something else'
end if

我认为此代码比建议的其他答案更清晰,并且您(通常)不需要为每个验证使用不同的变量,这是对其他问题的原始答案的改进。顺便说一句,您需要的每个新条件仅增加了一行代码。

答案 6 :(得分:0)

解决缺失值的技巧可能会有所帮助:

Dim passed, wrongMaxPercent, wrongPercent, rightMinPercent, rightPercent
wrongPercent = 33
rightPercent = 55

'rightMinPercent = 56
wrongMaxPercent = 40

passed = (Len(wrongMaxPercent) = 0 Or wrongPercent < wrongMaxPercent) And _
         (Len(rightMinPercent) = 0 Or rightPercent >= rightMinPercent)

答案 7 :(得分:0)

可以使用Or运算符来切换逻辑条件,并关闭如下错误消息:

Err.Clear
On Error Resume Next
If myObject Is Nothing Or Not myObject.test() Then
    Err.Clear
    'do something else'
Else
    'do something'
End If
On Error Goto 0 ' or On Error Goto ErrorHandler (depending on previous setting in the code)

没有什么测试是不必要的-它仅用于阐明含义。