How Do I Update a Variable? (Magic xpa 3.x)
There are several ways a variable can get updated in Magic xpa:
-
A default value may have been assigned to the field at the Model or Data Source level, or in the current task.
-
An Init may have been coded for that variable.
-
If the variable is on the form, the user may have typed in a new value.
-
An Update operation was executed.
-
One of the more advanced functions, such as the VarSet function, may have been used.
-
Data was read during an I/O operation.
Here we are going to cover the Update operation, which is the usual way to change a variable’s value from within a logic unit. The Update operation is procedural: you control exactly when it happens within a logic unit.
Much of the time, in online programs, the Init column is used instead of an Update. This is because an Init happens non-procedurally; the value is automatically updated whenever any of the values in the expression change. This saves you from having to figure out “when” you should do the update.
In this example, we are updating a “character count” field with the number of characters in a text field. The character count variable gets updated as soon as the user leaves the field, because we are coding it in the Control Suffix logic unit.
-
Go to the logic unit you want to use.
-
Press F4 (Edit->Create line). A blank line will appear below your cursor position, and your cursor will be located on a field at the left side of the new line.
-
Type U. The Update Variable operation will appear, and the cursor will move to the right.
-
Press zoom (F5 or double-click) to bring up a list of variables. Select the variable you want to update, by moving the cursor to that line and pressing Enter or clicking the Select button. You can also just type in the letter of the variable.
Press Tab to jump to the next field to the right, which is marked With:
-
Zoom (F5 or double-click) from the With: column, which will bring you into Expression Rules.
-
Type in an expression you want to use to update the variable with. If you just want to update one variable so it matches another variable, then just enter the variable. Or, the expression could be a number, like zero, if you are resetting a counter. Or it could be an expression like we used here, which returns the number of characters in a string (minus whatever blanks might be at the end).
-
Press Enter or click OK to “bring back” the expression number into the With: field.
How Do I Set the Value of a Task Variable?
The Online and Rich Client Samples projects (program LG06 and RLG06)