Framework resets public control properties AFTER constructor
This is from the user’s guide:
Initializing Control Subclasses
If you need to initialize an instance of a control subclass, dont use the constructor. Instead, put the code that does the initialization in the Open event of the control.
This is an issue we’re aware of and we actually are woking on a way to correct
Its caused lots of angst over the years
The big reason it is this way is the IDE generates all the necessary code for creating the layout and at some point it does something roughly like
dim c as new custom_control // your constructor gets run HERE
c.setAllPropertiesAsSetInTheIDE // your constructors effects get overwritten
and eventually all the open events get run
This is very specifically why constructors for control instances on a layout don’t work as expected
I think the OP was about something different, though I’m not entirely sure.
Create a project.
Create a class.
Create a property in that class.
Set a default value for that property in the Inspector.
Drag the class onto Window1.
In Window1 create the Open event.
Add the BREAK statement in the Open event.
Run the application.
Inspect the value of the property - it is of course the same as the default value for that property set in the Inspector.
Stop the application.
Go back to the class.
Change the default value of the property in the class definition (again in the Inspector).
Run the application again.
Inspect the value of the property - it is still the same as the first entered default value.
Even if you delete the default value in the Inspector, it will forever be the first-ever entered value. You have to delete the instance of the class on the window, and create a new one to get rid of that.
[quote=119475:@Eli Ott]I think the OP was about something different, though I’m not entirely sure.
[/quote]
The same effect can be seen if you do this & then go back to the original class & add a new event and try to implement it immediately on your layout where you have the instance
I know we have done some work on the event issue - not sure if that will also affect this one
It might
Uh oh, didn’t think that this caused such a long and heated discussion.
In real world I have a big class with event definitions and a HTTPSocket property, which I initialize in the constructor. In the beginning the instance of the HTTPSocket is created. Then I program a little bit more, compile several times and suddenly it’s nil when I try to use the HTTPSocket object. The only solution is to delete the class-object (the one I dropped in my window) and drag it again to the window.
I couldn’t really reproduce the behavior, only that it is not initialized. I now understand the process when a class is created. But set values ??in the open event of the control (in my case Window) is a little bit strange not?
You can circumvent the problem by creating a property in the window, creating the instance in the window’s Open event and using AddHandler to install event handler methods to react to the events of your instance.
What counts is to find a way that works Open it is until constructor properties become perenne.
That’s what I did. Not that comfortable but acceptable
Thanks to all for the detailed explanations.