Transferring StaticVar data to array elements?

Hello all,

I'm still learning StaticVar and Gui. I'm trying to assign 'data' from staticvar to 'myArray' elements like the example below (took 1 day to get the other part to work...phew..). Somehow the values are not transferring.

Appreciate any advice from you guys.

idCount = 1;
idReset = 2;
idchangeText = 3;
count = StaticVarGet("countStaticVar");
data = StaticVarGet("dataStaticVar");
myArray = 0;

function CreateButtonGUI()
{
	GuiButton( "Count+", idCount, 100, 600, 70, 30, notifyClicked );
	GuiButton( "Reset", idReset, 160, 600, 70, 30, notifyClicked );
	GuiButton( "Change Count Button", idChangeText, 100, 630, 130, 30, notifyClicked );
}


function Counter()
{
	count = Nz(count) + 1;
	StaticVarSet("countStaticVar", count);
	data = Nz(data) + 10;
	printf("\ndata = :" + data);
	myArray[count] = StaticVarSet("dataStaticVar", data);
}

function Reset()
{
	StaticVarRemove("countStaticVar");
}

function ChangeButtonText()
{
	GuiSetText("Counter", idCount ); 
}

function HandleEvents()
{
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
		
		switch ( id )
		{
			case idCount:
				Counter();
			break;
			
			case idReset:
				Reset();
			break;
			
			case idChangeText:
				ChangeButtonText();
			break;
		}
	}
}

CreateButtonGUI();
HandleEvents();

printf("\ncount: " + count);
printf("\nmyArray[2]: " + myArray[2]);

2019-11-03_20-20-56

Just detected some mistakes in my code. I updated the code like below.
Still could not assign the value properly from data to 'myarray'.

idCount = 1;
idReset = 2;
idchangeText = 3;
count = StaticVarGet("countStaticVar");
data = StaticVarGet("dataStaticVar");
myArray = 0;

function CreateButtonGUI()
{
	GuiButton( "Count+", idCount, 100, 600, 70, 30, notifyClicked );
	GuiButton( "Reset", idReset, 160, 600, 70, 30, notifyClicked );
	GuiButton( "Change Count Button", idChangeText, 100, 630, 130, 30, notifyClicked );
}

function Counter()
{
	count = Nz(count) + 1;
	StaticVarSet("countStaticVar", count);
	data = Nz(data) + 10;
	printf("\ndata = :" + data);
	//myArray[count] = StaticVarSet("dataStaticVar", data);
	StaticVarSet("dataStaticVar", data);
	myArray[count] = StaticVarGet("dataStaticVar");
}

function Reset()
{
	StaticVarRemove("countStaticVar");
	StaticVarRemove("dataStaticVar");
}

function ChangeButtonText()
{
	GuiSetText("Counter", idCount ); 
}
function HandleEvents()
{
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
		switch ( id )
		{
			case idCount:
				Counter();
			break;
			
			case idReset:
				Reset();
			break;
			
			case idChangeText:
				ChangeButtonText();
			break;
		}
	}
}
CreateButtonGUI();
HandleEvents();

printf("\ncount: " + count);

for(i=0;i<=5;i++)
{
	printf("\nmyArray[%g]: " + myArray[i], i);
}

2019-11-03_20-46-26

You add just number to entire array each time. Also array is visible add button click only so save to static var.

idCount = 1;
idReset = 2;
idchangeText = 3;
count = Nz(StaticVarGet("countStaticVar"));
data = Nz(StaticVarGet("dataStaticVar"));
myArray = 0;
printf("\ndata = :" + data);

function CreateButtonGUI()
{
	GuiButton( "Count+", idCount, 100, 100, 70, 30, notifyClicked );
	GuiButton( "Reset", idReset, 160, 100, 70, 30, notifyClicked );
	GuiButton( "Change Count Button", idChangeText, 100, 130, 130, 30, notifyClicked );
}

function Counter()
{
	count++;
	StaticVarSet("countStaticVar", count);
	data += 10;	
	StaticVarSet("dataStaticVar", data);
	myArray[count] = data;
	myArray += Nz(StaticVarGet("myArray"));
	StaticVarSet("myArray", myArray);
}

function Reset()
{
	StaticVarRemove("countStaticVar");
	StaticVarRemove("dataStaticVar");
	StaticVarRemove("myArray");
}

function ChangeButtonText()
{
	GuiSetText("Counter", idCount ); 
}

function HandleEvents()
{
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
		switch ( id )
		{
			case idCount:
				Counter();
			break;		

			case idReset:
				Reset();
			break;			

			case idChangeText:
				ChangeButtonText();
			break;
		}
	}
}

CreateButtonGUI();
HandleEvents();

myArray = StaticVarGet("myArray");

printf("\ncount: " + count);
printf("\nmyArray[2]: " + myArray[2]);

Plot( myArray, "Price", colorRed );

2

3 Likes

Superb. Thanks @fxshrat!

Hello @fxshrat and all,
As a continuation, I'm changing myArray to have it's index to follow barindex. However, the assigned number will be stacking up. I want it just to replace the existing value instead of adding up the value in a a selected bar.

I tried adding this code but that wont do it:

if(myArray[sbi] == 0) 
	{
		myArray[sbi] = data;
	}

Any advice is appreciated.

The full code:

//https://forum.amibroker.com/t/transferring-staticvar-data-to-array-elements/15489/3

idCount = 1;
idReset = 2;
idchangeText = 3;

bi = BarIndex();
sbi = SelectedValue(bi);

count = Nz(StaticVarGet("countStaticVar"));
data = Nz(StaticVarGet("dataStaticVar"));
myArray = 0;
printf("\ndata = " + data);

function CreateButtonGUI()
{
	GuiButton( "Count+", idCount, 100, 10, 70, 30, notifyClicked );
	GuiButton( "Reset", idReset, 160, 10, 70, 30, notifyClicked );
	GuiButton( "Change Count Button", idChangeText, 100, 40, 130, 30, notifyClicked );
}


function Counter()
{
	count++;
	StaticVarSet("countStaticVar", count);
	data += 10;
	printf("\ndata = :" + data);

	StaticVarSet("dataStaticVar", data);

	if(myArray[sbi] == 0) 
	{
		myArray[sbi] = data;
	}
	
	myArray += Nz(StaticVarGet("myArray"));
	
	StaticVarSet("myArray", myArray);
}

function Reset()
{
	StaticVarRemove("countStaticVar");
	StaticVarRemove("dataStaticVar");
	StaticVarRemove("myArray");
}

function ChangeButtonText()
{
	GuiSetText("Counter", idCount ); 
}

function HandleEvents()
{
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
		switch ( id )
		{
			case idCount:
				Counter();
			break;
			
			case idReset:
				Reset();
			break;
			
			case idChangeText:
				ChangeButtonText();
			break;
		}
	}
}

CreateButtonGUI();
HandleEvents();

printf("\ncount: " + count);
printf("\nmyArray[%g]: " + myArray[sbi], sbi);

myArray = StaticVarGet("myArray");
Plot( myArray, "Price", colorBrightGreen, styleNoLabel );

2019-11-05_09-17-29

I think I solved the stacking issue by doing this:

function Counter()
{
	count++;
	StaticVarSet("countStaticVar", count);
	data = 10;
	printf("\ndata = :" + data);

	StaticVarSet("dataStaticVar", data);
	
	myArray = Nz(StaticVarGet("myArray"));
	myArray[sbi] = data;
	StaticVarSet("myArray", myArray);	
}

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.