OnChange
new to Analytica 4.6
The OnChange attribute of a variable contains an expression that gets evaluated when the Definition of a variable changes.
When a variable is defined as a Choice or Checkbox, the expression is evaluated when the user changes the selection. For a Table, the expression is not evaluated when individual cells are edited, but it is evaluated when the final edit is made (e.g., when the green checkbox is pressed or edit table window is closed).
The expression can contain side-effects. For example, within the expression, you can assign to a global variable using the := operator. Such side-effects are not permitted in variable definitions. The result of the evaluation is not saved and is never used. Hence, the whole purpose of the expression is to enact some sort of side-effect.
The expression syntax is the same as the syntax for a Definition attribute.
OnChange replaces the Script attribute which was used for Choice and Checkbox controls prior to Analytica 4.6. The Script attribute is still present and still works (so that legacy models still work), but has the disadvantage that is uses Typescript, which meant that you had to learn something new. Since OnChange uses expression syntax, you don't have to learn something new. When both OnChange and Script are present, OnChange evaluates before Script. OnChange is more general than Script is/was in that it can be used with any variable, including as textbox inputs.
The OnChange expression is always evaluated in mid-mode.
Before you can access the OnChange attribute for a variable in the Attribute Pane or Object Window, you need to turn on the attribute visibility from the Attributes Dialog on the Object menu.
Assignment
When you change the Definition of a variable using the Assignment Operator, :=, the OnChange expression for that variable evaluates, unless it is already running. The fact that it doesn't run a second time if already running enables some interesting capabilities.
An OnChange attribute can change its own Definition. As an example, suppose that a variable X has the following as its OnChange expression:
X := "" & (Definition of X)
When a user enters a value, it is always converted to text. When the value is first changed, OnChange runs. It in turn alters the value again, but OnChange is not launched a second time because it is already running.
Two or more input variables can change each other via their OnChange attributes to keep them mutually consistent. For example, suppose you have two inputs variables, GMT_Time
and Local_Time
, and you have a variable named Time_Offset
that relates the two. You set
OnChange GMT_Time : Local_Time := GMT_Time + Time_Offset OnChange Local_Time : GMT_Time := Local_Time - Time_Offset
Now a user enters a time into the GMT_Time input. Local_time immediately adjusts. Or he enters a time into the Local_time input -- GMT_Time immediately adjusts. In the first case, OnChange of GMT_Time
evaluates, which changes the definition of Local_time, which in turn triggers OnChange of Local_Time
. Local_time actually changes the definition of GMT_Time again, but to the same value it already has, but OnClick of GMT_Time
does not run again because it is already active; hence, the loop terminates.
Inputs and Outputs
If the OnChange attribute of X uses the value of Y, then Y will show up on the list of Inputs' for X in the Object Window, and X will be in the list of Outputs for Y. If Z is assigned to in the OnChange attribute of X, then Z will appear as an Output of X and X will appear as an Input of Z. However, on the diagram, arrows are not show to depict these dependencies.
Enable comment auto-refresher