2015-08-20 16 views
7

Sadece bir Or koşullu deyiminin VBA/VB6'da nasıl çalıştığını merak ediyorum. Temel olarak, eğer If A Or B Then varsa, Boole ifadeleri hangi sırada değerlendirilir? A doğruysa B de değerlendirilir mi?VBA'da bir Or ifadesi nasıl değerlendirilir?

+0

her ikisi de dikkate alınmadan değerlendirilecektir. VBA'da, etraftaki tek yolun, ikisini de deyim halinde iç içe geçmiş iç içe bölmek olduğuna inanıyorum. –

+0

https://msdn.microsoft.com/EN-US/library/office/gg264205.aspx – findwindow

+0

Önceden: http://stackoverflow.com/questions/24641923/vba-short-circuit-and-alternatives, http://stackoverflow.com/questions/3242560/andalso-orelse-in-vba/3245183#3245183 –

cevap

5

İşte size bazı test sonuçları şunlardır:

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 

Yani, bir Or veya And hem ifadeler her zaman değerlendirilir, sırayla, sonuç ne olursa olsun. Basit inline kısa devre sağlamak için If ... Then If ... Then'u kullanabilirsiniz.