StaticVarRemove not removing static variables

With a Chart formula, I want to use a parameter to initialize a static variable.
But each time I change my "Instance Number", both both the old static variables and new ones are shown. How can I remove the original static variables?
Here is my code and screenshots for the results I am seeing.
(I should only see one "Section ID" value.

 

StaticVarRemove("");
Instance 		= Param("Instance Number",1,1,100,step=1);
SectionName = "Section ID" + Instance;
_Section_Begin(SectionName);
  ShowSlow 		= ParamToggle("Show MA Slow Line", "No|Yes", defaultval = 1);
  StaticVarSet("ShowSlow"+Instance, ShowSlow);  
_SECTION_END();

image

image

Hi @SwingTradeMonkey,

The section name must be a constant:

https://www.amibroker.com/guide/afl/_section_begin.html

@bigalgator Thank You! It now works!

Is there an AFL Declaration statement for setting the value of a constant (so the compiler can pick up on an attempt to reassign a new value to the variable holding the constant)?

Are there any other approaches to dynamically changing a section name based on a change in the Param statement’s resulting value?

Again, thanks!

Here is the final working result snippet:

StaticVarRemove("");
//Instance 		= Param("Instance Number",1,1,100,step=1);
Instance    	= 3;
SectionName = "Section ID" + Instance;
_Section_Begin(SectionName);
  ShowSlow 		= ParamToggle("Show MA Slow Line", "No|Yes", defaultval = 1);
  StaticVarSet("ShowSlow"+Instance, ShowSlow);  
_SECTION_END();

You’ll get unexpected behavior if you change a section name at runtime. The name should be a string enclosed in quotes, not a variable.

1 Like