PDA

View Full Version : private static variables are shared?


roland_nt
03-24-2010, 10:49 AM
I have a bug which seems to go away when I change
private static int varx;
to
private int varx;
but only if the same indicator is used in another chart.
varx seems to pick up the values of varx in any other chart having that indicator after a while, even if I reset varx to 0 on FirstTickOfBar in each instance.
Is the static area shared between multiple instances of the same indicator in different charts?
I know that this might still be my bug in other ways, but the issue is very repeatable and can be made to show up in the output window.
I do not actually need the "static" property one way or the other for the code to otherwise work. It was an artifact of earlier design.
I am using version 6.5.15 but has happened in earlier versions.

NinjaTrader_Bertrand
03-24-2010, 11:11 AM
roland_nt, using static / global variables in C# is unfortunately beyond what we can offer support for, however aspects and ways how to proceed have been discussed already here on our forums -

http://www.ninjatrader-support2.com/vb/showthread.php?t=4230

Ralph
03-24-2010, 02:04 PM
...Is the static area shared between multiple instances of the same indicator in different charts?...

Yes, this is correct, or even more precisely: A static variable is shared by all instances of the same class. Use a static element only when really needed.

Regards
Ralph

roland_nt
03-25-2010, 03:43 PM
Ralph:
Thanks for confirming my suspicion. It helps me to improve other indicators I have written as well and to understand exactly when "static" attribute might be properly used for an indicator. Really appreciate your stopping by to comment.
Roland