gpt4 book ai didi

vba - VBA 中如何计算 Or 语句?

转载 作者:行者123 更新时间:2023-12-02 01:58:03 24 4
gpt4 key购买 nike

我只是想知道 Or 条件语句在 VBA/VB6 中如何工作。基本上,如果我们有 If A Or B Then,那么 bool 表达式的计算顺序是什么?如果 A 为真,B 也会被评估吗?

最佳答案

以下是一些测试结果:

Public Sub DoTesting()

' Displays "Test1" followed by "Test2", so they're evaluated in order.
' Also, both expressions are evaluated even though Test1() is False.
If Test1() And Test2() Then
End If

' Displays "Test2" followed by "Test1", so they're evaluated in order.
' Also, both expressions are evaluated even though Test2() is True.
If Test2() Or Test1() Then
End If

' Displays "Test1" only. Test2() is not evaluated.
If Test1() Then If Test2() Then Debug.Print ""

End Sub

Public Function Test1() As Boolean
MsgBox "Test1"
Test1 = False
End Function

Public Function Test2() As Boolean
MsgBox "Test2"
Test2 = True
End Function

因此,无论结果如何,OrAnd 中的两个表达式始终按顺序求值。您可以使用 If ...Then If...Then 来实现简单的内联短路。

关于vba - VBA 中如何计算 Or 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32125403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com