external static variable

To add comments, please log in or register
shonick
81
shonick  

The situation is, on the initial run, I set that number on the input. After EA run, it meet certain criteria, it will reset that variable to be 0, and keep it 0 until I change the input variable again to be something else. Is there anyway to do that?

Nathan
1434
Nathan  

Hello shonick,

Yes, use static memory class to "freeze" the value assigned to a designated datatype until your program arrives at a condition which assigns the "frozen" static variable with a new value.

I almost threw my laptop Betsy out'a window once, cuz I didn't understand why my Boolean datatype were changing their values. VERY frustrating! ;)

Also, I don't believe I've been able to code an external static variable. I assume 'external' is considered 'static' by default. If it is, (which I'm sure someone will stroll by this post and confirm or deny) there shouldn't

be a reason to assign static memory type to your external variable.


Thank you

William Roeder
22732
William Roeder  

Common (globally declared) variables including externals and (locally declared) static retain their values between calls.

Remember, they are not reset on deinit/init cycles (e.g. chart change.) only on load (new chart, recompile, etc.) Thus I use this paradigm:

Static not reset
TYPE Function(){
   static TYPE variable = 0; // Not reset on chart change.
   if (variable == 0){ variable = ...
Resetting static
int init(){
   OnInitFunction(); OnInitFunction2 ..
   :
}
TYPE functionVariable; void OnInitFunction(){ functionVariable = 0; }
TYPE Function(){
   if (functionVariable == 0){ functionVariable = ...
Ahmed Soliman
25017
Ahmed Soliman  
shonick:


 

The situation is, on the initial run, I set that number on the input. After EA run, it meet certain criteria, it will reset that variable to be 0, and keep it 0 until I change the input variable again to be something else. Is there anyway to do that?


External variables are statics. Once your code set it to 0 it retains 0 until the user/terminal re-load the EA.

Nathan
1434
Nathan  
WHRoeder:

Common (globally declared) variables including externals and (locally declared) static retain their values between calls.

Remember, they are not reset on deinit/init cycles (e.g. chart change.) only on load (new chart, recompile, etc.) Thus I use this paradigm:

Static not reset
Resetting static

Based on my experiences, any datatype without declaration of static memory class is reset to zero once the condition (assigning the datatype variable a value) returns false. This is why Boolean variables almost drove me to "coding madness" when these Boolean variable values returned false once conditions returned false. Unbelievably frustrating :) Hahaha!

This must be because these datatype variable values aren't saved between program execution periods. Hmm.

 

Thank you

To add comments, please log in or register