How Do I Set the Value of a Task Variable? (Magic xpa 3.x)
There are several different ways that variables get modified in Magic xpa. These can be divided into a few categories:
-
Values that get initialized automatically when the record is created. These include the Default Value properties, and also the Init property.
-
Values that get moved in as part of the Operations of the program. This would include the Update operation, VarSet, and values that get passed back in arguments.
-
Values that get changed by the user, when they type in a value or work with a control.
Here is a summary of how each of the options work.
The Default Value Property
Any field can have a default value property. This property can be assigned at any level: in the field Model, in the Data Source, or within the task. If the field has a default value, then when the value will be used to initialize the field before the user sees the record.
The Init Property
In addition to the Default value property on the field definition, you can also use the Init value when you select the field in the task Data View.
An Init works slightly differently from a Default value. Here is a comparison of the two:
|
|
Initializes a new field
|
Initializes a new field
|
Uses an Expression
|
Uses a hard-coded value
|
Enables re-calculation, if any of the values in the Expression change.
|
Only uses the hard-coded value
|
If you want a field to always be initialized to the same value, the Default value property is the best way to go.
Update Operation
The most common way that variables are updated in a task is by using the Update operation. You can enter an Update operation in any logic unit.
The syntax of the Update operation is shown above. It can also be explained as:
Update <variable> with <expression> if <Cnd>
The items in brackets are the ones you enter, mainly by zooming to the variables list or Expressions Rules, respectively.
VarSet()
The VarSet() function is one of the few functions that allows you to update an variable directly from within the Expressions Rules. It is very powerful, and has been used for many purposes by developers over the years.
The syntax of VarSet() is:
VarSet(<variable>, <value>)
Where <variable> is entered as a VAR literal. A VAR literal is entered as, say, ‘P’VAR, where ‘P’ is the variable ID. If the variables in a task change position, and ‘P’ turns to ‘Q’, then the expression will be automatically updated.
<value> can be any expression that evaluates to the proper value. It can be hard-coded, or it can be a variable, or it can be some function.
So for instance:
VarSet('P'VAR, X+6)
Will update the variable P to whatever was in X, plus 6.
VarSet gets more interesting because it can be used to access variables as though they were in an array. So for example:
VarSet('P'VAR+1, X+6)
Will update variable Q to whatever was in X, plus 6.
Note: VarSet is used in very specific instances, where a dynamic variable reference is required. Usually, a regular Update operation is used for updating variables.
Of course, variables can be changed by the user. A variable that can be changed by the user has to be displayed on the form in a control with the Modifiable and Allow Parking properties set to Yes.
The Online and Rich Client Samples projects (program DV10 and RDV10)