0

I wonder when Properties are processed in Office VBA.

Consider this Class module (MyClass):

Public Property Get ExpensiveProperty() As Variant
    'Some resource expensive procedure here
End Property

Public Property Get SomeProperty() As Variant
    'Something easy
End Property

And this Module:

Sub test()
    Dim MC As MyClass
    Set MC = New MyClass
    
    Dim Smth As Variant
    Smth = MC.SomeProperty
End Sub

Is MC.ExpensiveProperty processed in test() procedure? (Assume no reference to ExpensiveProperty in SomeProperty)

4
  • 1
    I don't this so (why should it?), but why don't you check with the debugger?
    – FunThomas
    Apr 7, 2021 at 9:06
  • When debugging, all properties are computed. So it is not so simple. I'm not sure if there is some trigger (for example: When Property procedure access Range Object it is always processed even if not used <- I made up this condition).
    – Lluser
    Apr 7, 2021 at 10:12
  • 1
    No, properties are not accessed when debugging, properties are accessed when you add the object to the watch window. When simply debugging (by setting a breakpoint), you will see that properties are only entered when you request them. There is no functional difference between a property and a method, so expecting a property to be randomly accessed is no different to expecting all methods to be randomly called, which does not happen.
    – GSerg
    Apr 21, 2021 at 13:39
  • @GSerg: Thanks for reply. I received "mysterious" error while debugging, I found it was caused by an unused property. So I was uncertain how it works. But maybe it was called by some forgotten watch.
    – Lluser
    Apr 22, 2021 at 6:20

1 Answer 1

1

Putting a simple Debug.Print-Statement reveals that ExpensiveProperty is not called except if requested (and I see no point why it should).

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

After running your test-routine, you will find the output Easy... in the immediate window but not Expensive.... Setting a breakpoint in both getter also shows that the expensive method is not called.

Of course that changes if you add a watch on the MC-variable or look into it in the Local-window. In the moment you click on the small [+]-button, the debugger tries to evaluate all public properties and for that of course must call all getter methods (note that in that case the debugger will not stop at any breakpoint, but the Debug.Print is executed.

1
  • Thanks for Answer. I received "mysterious" error while debugging, I found it was caused by an unused property. But maybe it was called by some forgotten watch.
    – Lluser
    Apr 22, 2021 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.