Support Global/Local Variables and Static Global Variable
Local variable is almost the same as the global variable (e.g., the supported data types, etc.) except for the application scope. Global variable can be used in all diagnoses, while local variable can only be used in the diagnosis it belongs to, not visible in other diagnoses.
In addition, global variables can also be set as static variable.
The basic information about local and global variables can be summarized as follows.
Variable Category | Application Scope | Life Cycle | Supported Data Type | Constant | Support Data Source |
Local Variable |
Its value can be changed in diagnosis logic. | NI Runtime | int, float, string, table, bool, object, list | No | No |
Global Variable | Can be used in:
Its value can be changed in diagnosis logic. | NI Runtime | int, float, string, table, bool, object, list | No | No |
Local Variable
Local variable is defined in the diagnosis, and its scope is only the current diagnosis.
- Its data type can be int, float, string, object, table, list, or table.
- You can set values for local variables just like for global/task variables.
- Both the parser variable and compound variable support selecting data source (current/baseline/last), while the local variable, intent global variable, and task variable do not support this.
Compared with global variables, local variables offer several advantages, particularly in terms of code maintainability, readability, reliability, and performance. Here are the key benefits:
- Reduces Naming Conflicts
Local variables are limited in scope to a specific diagnosis, isolating them from other diagnoses of the intent and reducing the chances of naming conflicts. Global variables, accessible throughout the entire intent, are more prone to conflicts and accidental overwrites. - Improves Readability and Maintainability
Local variables have a limited lifespan, only existing within their specific block. This makes the code easier to understand and maintain, as developers can focus on a variable's behavior within a specific diagnosis. Global variables, which can be modified anywhere in the program, make the logic harder to trace. - Enhances Memory Management
Local variables are automatically destroyed after use, freeing up memory. Global variables remain in memory for the intent's entire runtime, potentially increasing memory usage and the risk of memory leaks. Using local variables helps manage memory more efficiently, especially in large intent. - Reduces Unintended Modifications and Increases Reliability
Since global variables can be accessed and modified from anywhere in the intent, unintended changes can introduce difficult-to-debug errors. Local variables, with their limited scope, reduce this risk, increasing the intent's overall reliability.
Set Global Variable as Static Variable
Compared to the previous version, the global variable is enhanced:
- Global variable supports more data type via adding the types of object, list, and bool. The original number type is changed to int and float.
- The column variable of the global table adds support for bool.
Additionally, when you define a global variable, you can set it as a static variable. Then its value will be automatically saved at the end of each intent execution, and the saved value will be automatically loaded for you to use at the beginning of the next intent execution. Therefore, you can perform historical analysis and comparison on specific variables.
All types of global variables can be set as static variables, including single variable, list, table, and object.
- For the object and list of objects, what is stored is the object ID, not the properties.
- For the table, to protect the system, there is a limit on the maximum number of the stored rows, controlled by ‘MaxRowOfStaticTable’. ‘MaxRowOfStaticTable’ is 10000 by default and can be configured in the database.
If a global variable is set as a static variable before, now it is edited and no longer a static variable, its stored values will be automatically cleared when saving NI.
How to Define Local/Global Variable
The input of the UI will change depending on the selected variable type.
- For int, float, string, bool, and object, users need to enter a variable name and specify the data type.
- For int, float, bool, and string, users can optionally specify the initial value. The initial value can only be static and cannot be assigned using expression.
- For table, users need to enter a variable name and specify the data type. The row definition is optional, but they must define the columns with variable name and type.
Note: Global table and local table do not support the columns with the types of object, device list, and interface list. While ADT supports this.