<< Click to Display Table of Contents >> Navigation: Several practical recommendations > Using Variables in Tasks > Local Variables |
What local variables are
Local variables of a task are custom variables that "live" only while the task is running.
•Differences between local and global variables:
•Local variables "live" only while their task is running. Unlike local variables, global variables live starting from RoboTask startup (or since such a variable is created) and until RoboTask is exited or until such a variable is deleted.
•The values of local variables are not preserved till the next session of working with RoboTask.
•Local variables can be edited and accessed only within the running task. Global variables can be accessed and edited in any task.
They are similar to global variables in all other aspects.
Advantages of using local variables:
We do not have to take care of deleting unnecessary variables when the task is complete because local variables are deleted from memory automatically when their task is complete.
When you use local variables, you do not have to worry that they will be changed by another task. And vice versa: the use of local variables guarantees that the values of variables will be preserved while the task is running.
If several tasks that can change one and the same variable are running simultaneously, the result may be unpredictable. Suppose we have two similar tasks that go through files in a loop, but they do it in different folders. Both tasks assign the name of the current file to the CurrentFile variable. If these tasks are running simultaneously, we may end up with quite a few problems. It is easy to avoid it if you declare the CurrentFile variable as local in both tasks.
Such a situation is not a rare one, especially if you have several hundreds of tasks. You can also create new tasks by copying an existing one and changing a few steps in it.
How to declare local variables
You can declare local variables on the "Local variables" tab of the task editor. There are two ways to declare them:
Enumerate the names of the variables;
Assign initial values to them.
If you enumerate them, just fill the list of names for local variables: one variables per line. For example, like this:
LineCount
FileToUpload
CurrentFile
etc.
To assign an initial value, specify this value after the "=" character. For example, like this
LineCount = 0
FileToUpload = c:\incoming\list.txt
CurrentFile = note.txt
etc.
You can assign initial values not to all variables. By default, local variables contain an empty value (an empty string).
By default, the list of local variables is empty. It means that all variables that are used in a task will be processed as global ones when you run the task.
How to use local variables
There are no additional requirements regarding the use of local variables. The same tools are used to work with local variables as with global ones:
Actions from the Variables group
Inserting variables into strings in various actions.
For example:
Current folder is "{CurrentFolder}"
Using the methods of the RoboTaskApp object: ExpandText, SetUserVariable, etc.
The rules according to which RoboTask processes local variables are very simple:
Once a variable is called, RoboTask first tries to find it among the local variables of the task. Calling a variable means any call: assigning a new value or calculating the variables value when macro insertions with the help of variables are substituted.
If the local variable is declared, further operations (reading or assigning the value) will be applied to the local variable. Otherwise, RoboTask will process the variable as a global one.
Thus, if we declare a local variable that has the same name as a global variable, the local variable will completely override the global variable. The running task will not simply see the global variable.
It is better to declare all internal variables as local ones for most tasks. As a rule, these variables are used in a loop or to pass data from one step in the task to another. When you use local variables, you need not worry that the new task will mess up data from other tasks. And vice versa – that some other task running simultaneously changes the data of the current task.
On the other hand, if you need to pass some data to another task, you should make sure that the necessary variables are absent in the list of local variables.
Related Topics
Saving of a result into a variable
Rules for using variables in expressions
How to use variables correctly