Got a problem with Xojo 2020 r2.1
I have a class, subclassed from Canvas. It contains methods to perform averaging of supplied values, and then graph those values in the canvas instance. I chose to store the supplied values within the (private) class method, as static double arrays.
static myValuesToAverage as double(5)
I make three instances of this class, by putting them on the window (form) at design time. I supply them with different data at run time.
When the data is plotted without averaging (i.e. not using the static double array) the points are plotted correctly.
When the data is plotted with averaging, the three classes display the same data ( ! )
I can only conclude that the static variables are shared across all instances of the class.
I then refactored to use properties of the class (instead of static arrays of doubles within each method).
This works correctly.
Is this a known bug ?
What you describe is a feature and not a bug. Having the same value is the purpose of a static value.
Thank you Beatrix. From your answer I conclude that a static variable in a method within a class will be effectively singular; that its contents will be accessible to all instances of that class.
Thus, we have a class called specialClass, with one method foo.
Within foo there is a static double called rememberMe.
Function foo( dv as double) as double
static rememberMe as double
if dv < 0 then rememberMe = 0
rememberMe = dv + rememberMe
The function sums every positive number and returns it.
When passed a negative number it resets (zeroes) the function memory.
And we create two instances of specialClass, myFirst and mySecond, dragged onto the window to instantiate them at runtime.
In the course of running this project, we run this code from a button Action event.
dim a, b, c as double
a = myFirst.foo(10)
b = mySecond.foo(20)
c = myFirst.foo(10)
Now we should expect that c = 20, because variables inside a class are cloned at instantiation, rather than shared …
But c equals 40, showing that myFirst’s rememberMe (declared inside a method, and supposedly private to that method) is shared by all members of all instances of specialClass.
I am perplexed by this behaviour. However I try I cannot see it as a feature
As it says in the docs:
A Static variable is equivalent to a local variable declared by the Var statement but its value is retained between several calls of the method it is declared in. Also, all the instances of a given class share the same static values.
Not sure why, but Static on a locally defined variable effectively makes it a shared property, whereas you want an unshared property, which is what you stated you wound up doing.
I don’t know how long you’ve been using Xojo, but using properties is the “standard” way of declaring a class variable that you want to be “remembered” for a class instance. Every class instance gets its own copies of non-shared properties that is kept until the class instance is destroyed. Plus, by defining it as a (public) property you can reference it outside of the class using dot notation, e.g. myClassInstance.myProperty. Of course, you can make it a private property to prevent that.
Thank You Jay and Beatrix.
I have been using Xojo for about twenty years, but have not encountered this issue before.
As it is documented, I shall continue.
Again, thank you to both.
Its not intuitive granted
you kind of expect that the static will retain its value across calls
but not that one declared in a method is shared across al instances of that method (and therefore across all instances)
but that is what it is
Thank you for this. I am glad that I am not the only person who finds this counter intuitive.
However I move on, prefer not to waste energy on such things, even if they caused me a couple of days of puzzlement.