This is the case:
When a class has a constructor, it can access any defined global variables (base types like boolean, string etc) in the application. It can set them and also read them.
The constructor is clearly executed before
So, based on what the constructor can do with global variables, we would expect when the constructor sets global variable values, they will be available at the point when
On SAM_AppStartupis executed.
Well, this is actually true ONLY when the global variable is defined BEFORE the class UDV object. When the sequence is different, where the variable is defined AFTER the class UDV object, the global variable is reset (or re-initialized) before SAM_AppStartup.
This is unwanted behavior as it can break application depending on the sequence of global variables. The sequence for base types should not matter.
Run it and see there are 2 messageboxes shown:
First msgbox: shown on the constructor of the class. It clearly shows that the global variable is set to TRUE and the constructor can read it.
Second msgbox is at SAM_AppStartup. It will read the global boolean and show the value in the msgbox.
Initially the testcase works like expected. The boolean is TRUE in both cases.
Now change the code. Move the global
gbInitDoneboolean variable AFTER the UDV variable named
In this case, the first msgbox still shows TRUE for the variable, but FALSE in the second msgbox.
The TD runtime is initializing the variables in sequence of definition so it seems.
So when the UDV constructor has set the global variable, the TD runtime will initialize the global variable afterwards and clearing the previously set value.
IMHO, the TD runtime should be more intelligent and first initialize all base variables (booleans, strings etc) and then (in sequence) init all UDV variables.
When the TD runtime does this, the issue would be gone. All base types are initialized before any constructor is executed and is able to set values which will be available from the
SAM_AppStartuptime and further.
This has been registered as:
Ticket # 3041468: Global variables sequence has effect on initialization timing