Link Search Menu Expand Document

5.5 Auto-recalculation between objects

For objects included in the hierarchical structure starting with “//” , by connecting the objects with the reference operator (& =) , the value on the left side is automatically re-established according to the change in the value on the right side of the operator. You can calculate.

How to use the reference operator
Example Adjust the display position of the button according to the size of the window

Form Form1 {
    Width = 500;
    Height = 400;
    Button Button1 {
        X & = Form1.Width / 5;
        Y & = Form1.Height / 5;
        Width & = (Form1.Width / 5) * 3;
        Height & = (Form1.Height / 5) * 3;
    }
}

Form1 directly under “//” ( Root object) automatically follows the resizing of the window and changes the Width and Height properties. In this example, Button1 ‘s X , Y , Width , and Height are associated with Form1 ‘s Width and Height with the & = operator, so Button1 ‘s display position and size change as the window is resized. increase.

Example TextBox error display

Form Form1 {
    Width = 500;
    Height = 400;
    TextBox Text1 {
        X = 10;
        Y = 10;
        Width = 100;
        Height = 30;
        BgColor & = (Err == 0? $ STD: $ RED);
        Number Err {
            Value = 0;
        }
    }
    Button Button1 {
        X = 10;
        Y = 50;
        Width = 100;
        Height = 30;
        Function OnTouch (e) {
            if (Form1.Text1 == "") {
                Form1.Text1.Err = 1;
            } else {
                Form1.Text1.Err = 0;
            }
        }
    }
}

In this example, the event handler that reacts when Button1 is pressed sets Form1.Text1.Err to 1 if Text1 is empty, and 0 if it is not.

The background color of Text1 is related to Err by the & = operator , so it reacts red when Err is set to 1 .

By using the reference operator & = in this way, it becomes possible to write a script with a more essential process of “setting an error” instead of the process of “making the background red”.

Reference operator constraints

Only available in the object tree
The reference operator can only be used for objects contained in an object tree whose vertices are “//” ( Root object). It cannot contain anonymous objects referenced by var variables on either the left or right side of the reference operator, or user objects loaded in the global namespace.

Expressions and functions with update behavior cannot be written on the right side
Writing an expression with an update operation on the right-hand side of the reference operator is not supported by CRS because it makes the relationship dependencies very complicated . Operators that involve an update operation include the ++ and –operators .

User-defined functions cannot be written on the right side
A user-defined function defined by Function cannot be written on the right side of the reference operator .

Dependency analysis is performed by the symbols that appear on the right side
Global functions and methods that do not involve update behavior can be written on the right side of the reference operator. Since the CRS interpreter manages dependencies by relying on the symbols (object names and property names) that appear on the right side, recalculation is not executed when a value that changes secondarily by calling a method is described on the right side.