variables in calc fields

Just something I’d like to mention as I did not see it here.
I have a for loop which creates a few records, using a variable ‘i’.
These records have calculated fields; one of these has also a loop in its calc method using a ‘i’ variable; I found out they are not separate: during the record creation loop the variables are shared, the env is common so the calc field method modifies the “i” value of the creation loop method on the fly, nice unexpected effect which never ends!

Do you have a “var” declaration in the for loop? like:

for(var i = 1; i <= n; i++ )
{
    //
}

If not, you are declaring and using a global variable “i” which will be the same everywhere.

greg.

You mean it follows the same rules as other vars… makes sense, why didn’t I think about that? I never use local vars in for loops, but can’t say why… probably because php is more familiar, so I easily forget the ‘var’.
thanks a lot anyway

Always declare variables inside methods using var, or else they become variables in the global scope and live on after the method is finished, for the duration of the session.

so, if you do this all over the place and put fair amounts of data into the variables, your Client memory will become more and more.

Paul

I did take care of this in any other situation other than calc. fields, ie when a method was calling another one for instance. And I don’t use many globals as you can’t display them in forms, I use global fields instead. But the for loop was out of this principle, probably because you set it to some value to start with, so you rarely get problems; and it’s probably the first time I use a loop in a calc field (roman to arabic numbers conversion, no choice…).
thanks