AddVariable
Description: | Adds a new variable to an existing module and returns its variable value. |
Warning: | This function may cause irrecoverable alteration of your application. It should be used only by advanced programmers. |
Returns: | Variable |
Usage: | Script Only. |
Function Groups: | Compilation and Online Modifications, Variable |
Related to: | DeleteVariable | MakeNonShared | MakeNonPersistent | MakePersistent | MakeShared | SetDefault | SetVariableClass | SetVariableText |
Format: | AddVariable(Module, Name, Reserved, Attrib, Class, PersistentSize, VarTextSize, NumDimensions, ArrayElem, ArraySize) |
Parameters: |
Module | ||||||||||||||||||||||||||||||||||||
Required. Any expression that returns a module value. | ||||||||||||||||||||||||||||||||||||
Name | ||||||||||||||||||||||||||||||||||||
Required. Any text expression that gives the name of the new variable. | ||||||||||||||||||||||||||||||||||||
Reserved | ||||||||||||||||||||||||||||||||||||
Reserved for future use. Set to 0. | ||||||||||||||||||||||||||||||||||||
Attrib | ||||||||||||||||||||||||||||||||||||
Required. Any numeric expression giving the variable attribute bits as follows:
A variable cannot be both persistent and temporary, since a persistent variable is stored on disk, and a temporary variable exists only while VTScada is running or until the application is recompiled. * Note: Temporary variables should be used only by advanced users, since recompiling the application destroys them. |
Class |
Required. Any numeric expression in the range 0 to 65535, giving the variable class number for the new variable. |
PersistentSize |
Required. Any numeric expression giving the number of bytes of storage allocated in the .VAL persistent variable file for this variable. For array types, set this to the byte size of the largest array element (normally 8 bytes for numeric values). For arrays containing text, enter the character length of the longest string element. Use a valid 0 if this isn't a persistent variable. |
VarTextSize |
Required. Any numeric expression giving the length of the variable declaration text in characters. This parameter is ignored for temporary variable types. |
NumDimensions |
Required. Any numeric expression that gives the number of array dimensions for the variable. NumDimensions should be "0" for a simple variable. |
ArrayElem |
Required. Any array element giving the starting element in the array. The subscript for the array may be any numeric expression. If NumDimensions is 0, ArrayElem is ignored. If NumDimensions is 1, this specifies the starting index for the array. If NumDimensions is greater than 1, this is element of an array of starting indices for each dimension of the multidimensional array. |
ArraySize |
Required. Any numeric expression specifying the size of the array. If NumDimensions is 0, ArraySize is ignored. If NumDimensions is 1, this specifies the number of elements in the array. If NumDimensions is greater than 1, this is an array of sizes for each dimension of the array. |
Comments: | This function doesn't affect the .SRC file; it affects the expected location of items in the .SRC file. Both must be updated in unison. The return value is the new variable added. If the variable already existed, it will remain unchanged and the return value will be Invalid. Bit 10 (1024) of the Attrib parameter specifies that the variable is an instance variable. An instance variable is a temporary variable that gets added to a single instance of a module, rather than all instances of a module. If you specify this bit, then you must have passed an object (module instance) into the first parameter. Instance variables cannot also have the attributes Shared, Persistent, Module, or Parameter. |
Example:
If 1 Main; [ IfThen(! Valid(FindVariable(varName, Self(), 0, 1)) { Variable doesn't exist }, AddVariable(Self() { Create variable as part of this module }, varName { Name of new variable }, 0 { Reserved }, 1 { Variable is type array }, 0 { Class }, 0 { Variable not persistent }, 20 { Name is 20 chars long }, 1 { Var is 1 dimensional array }, 0 { Array starting index }, 220 { Number of elements in array })); ]
In the example above, the statement checks to see whether a certain (array) variable exists. If it does not, the statement creates a 1 dimensional array (named VarName) with 220 elements.