Timing of property initialization

I have a control subclassed from Canvas, with a property (“FilterN”) whose value is settable in the Inspector.

Screen Shot 2021-09-24 at 10.24.08 PM

I have a computed property that uses FilterN in a calculation.

An instance of the control is placed via the IDE on a window, whose ImplicitInstance property is set to false. In App.Open, I create a new instance of the window and call its Show method.

Two things are happening that I don’t understand:

The Setter of the computed property is being called somehow, and I’m pretty sure not by my code, as there is only one call to the setter in the entire program, and a breakpoint set there doesn’t fire in this case. If I breakpoint in the setter, the stack trace indicates the instantiation of the window by App.Open is the “caller”.

At the moment the Setter is called, FilterN has not yet been initialized to its inspector-set value, i.e. it’s 0.

What’s going on here? Why is the setter being called, and why is the FilterN property not initialized at that time? You’d think (hope) that default properties would be initialized before just about anything else.

The sequence of events is something like

Instantiate the Window
Call the Window Constructor

Window Constructor:
Instantiate each Control
Call Constructor on the Control, which
– Call Open on the Control
Repeat for each Control
Call Window.Open, which
– Initialize all the variables ← this is where the setter is called
Window Constructor finished

It would appear that the computed property is being initialized before FilterN. You’ll need some way to detect that, like check for FilterN being zero and skip it. Or just ignore this extra call if it doesn’t hurt anything.

Thanks, @Tim_Hare, that’s helpful!

That’s what I did (as you can see in the screen cap), but I wanted to have a better understanding of what I’m patching over before accepting it as a robust approach. Still not sure why it’s necessary for the setter to be called as part of the initialization, and if all setters are called during initialization, then that should probably be mentioned in the documentation.

2 Likes