Office VBA:属性什么时候处理?
Office VBA: When are Properties processed?
我想知道何时在 Office 中处理属性 VBA。
考虑这个 Class 模块 (MyClass
):
Public Property Get ExpensiveProperty() As Variant
'Some resource expensive procedure here
End Property
Public Property Get SomeProperty() As Variant
'Something easy
End Property
而这个模块:
Sub test()
Dim MC As MyClass
Set MC = New MyClass
Dim Smth As Variant
Smth = MC.SomeProperty
End Sub
MC.ExpensiveProperty
是在test()
程序中处理的吗? (假设在 SomeProperty
中没有引用 ExpensiveProperty
)
放置一个简单的 Debug.Print
-语句表明 ExpensiveProperty
不会被调用,除非被请求(我看不出为什么它应该被调用)。
Public Property Get ExpensiveProperty() As Variant
Debug.Print "Expensive..."
ExpensiveProperty = "Expensive"
End Property
Public Property Get SomeProperty() As Variant
Debug.Print "Easy..."
SomeProperty = "Easy"
End Property
在 运行 你的测试例程之后,你会在立即 window 中找到输出 Easy...
而不是 Expensive...
。两者都设置断点getter也说明没有调用昂贵的方法
当然,如果您在 MC
-变量上添加监视或在 Local
-window 中查看它,情况就会发生变化。在您单击小 [+] 按钮的那一刻,调试器会尝试评估所有 public 属性,为此当然必须调用所有 getter 方法(请注意,在这种情况下,调试器不会在任何断点处停止,但执行 Debug.Print
。
我想知道何时在 Office 中处理属性 VBA。
考虑这个 Class 模块 (MyClass
):
Public Property Get ExpensiveProperty() As Variant
'Some resource expensive procedure here
End Property
Public Property Get SomeProperty() As Variant
'Something easy
End Property
而这个模块:
Sub test()
Dim MC As MyClass
Set MC = New MyClass
Dim Smth As Variant
Smth = MC.SomeProperty
End Sub
MC.ExpensiveProperty
是在test()
程序中处理的吗? (假设在 SomeProperty
中没有引用 ExpensiveProperty
)
放置一个简单的 Debug.Print
-语句表明 ExpensiveProperty
不会被调用,除非被请求(我看不出为什么它应该被调用)。
Public Property Get ExpensiveProperty() As Variant
Debug.Print "Expensive..."
ExpensiveProperty = "Expensive"
End Property
Public Property Get SomeProperty() As Variant
Debug.Print "Easy..."
SomeProperty = "Easy"
End Property
在 运行 你的测试例程之后,你会在立即 window 中找到输出 Easy...
而不是 Expensive...
。两者都设置断点getter也说明没有调用昂贵的方法
当然,如果您在 MC
-变量上添加监视或在 Local
-window 中查看它,情况就会发生变化。在您单击小 [+] 按钮的那一刻,调试器会尝试评估所有 public 属性,为此当然必须调用所有 getter 方法(请注意,在这种情况下,调试器不会在任何断点处停止,但执行 Debug.Print
。