Local variables per form

Is it possible to have local variables per form?
Now we have local varibales per script.
And we have global variables per solutions.

But there are situations where I need local variables per form.
I would help to limit the number of global variables and to obtain a better overview of my solution

Yes, you can.

By simply not declaring them (var variable=blabla) but instead calling them like variable=blabla you have created a form variable.

BTW when you create a global without defining them in the dataprovider dialog they will live as globals but you will be able to maintain overview.

What Marcel points out is actually a dynamic global variable.

It persists beyond the execution of the method, but it’s scope is global.

To achieve a true form-level variable you could use the “this” keyword.

In a form method, assigning a variable to “this” will actually bind the object to the form. But be cautious, there are other objects bound the for that you might not want to overwrite, such as the controller and data-providers.

Here’s an example:

// local variable - it lives and dies in the method
var localVar= "local";

// form level variable - binds object to form 
this.formVar= "form";

//  dynamic global variable - dynamically creates a global variable, lives on after method terminates
dynamicGlobalVar = "global";

A subtle caveat is the precedence when referencing the variables.
The order is local, then form, then global.
…meaning that if you’ve defined variables of the same name, but at differing scopes, the local one will take precedence.
Example:

// create dynamic global
theVar = "global";

// create form-level
this.theVar = "form";

// create the local  scope
var theVar = "local";

// print out theVar
application.output(theVar); // it prints the local var

// you have to explicitly cite the scope if you want the form level
application.output(this.theVar);

Hope this helps,
Sean

Sean, you are correct :oops:

Thanks for this explanation. It is clear.
But it would be great if you could create form variables from the explorer treeview like creating global variables.
It will give a good overview in your solution

Hi Martin,

please have a look to this topic, think you’ll get some answers…

http://forum.servoy.com/viewtopic.php?t=8755

According to servoy, locals (form variables) may be introduced by version 4.

At that moment, we use a workaround, by defining a new property (private property) into the form object. We then stock information into that property. Hope this helps…

Looks like your request is the same.
I hope it will be introduced in version 4.
I want to avoid that programmers will use some variable which are form wide, but you can’t see it in the explorer treeview