set-value 块是一个代码区域,可在其中设置属性和字段值。有关背景,请参阅 EGL 属性概述。
在后两种情况下,仅对字段赋值。
在所有情况下,set-value 块在要修改的部件、变量或字段的作用域中。下面用示例对语法中的变化作最好的演示。
// the scope of the set-value block is myPart DataItem myPart INT { inputRequired = yes, align = left } end
// the scope is myVariable myVariable INT { inputRequired = yes, align = left };
// The scope is myRecordPart Record myRecordPart type SQLRecord { tableNames = [["myTable"]], keyItems = ["myKey"] } myKey CHAR(10); myOtherKey CHAR(10); myContent01 CHAR(60); myContent02 CHAR(60); end
// The scope is myRecord myRecord myRecordPart { keyItems = ["myOtherKey"], myContent01 = "abc", myContent02 = "xyz" };
// the example shows the only case in which a // record property can be overridden in a // variable declaration. // the scope is myRecord myRecord myRecordPart {keyItems = ["myOtherKey"]}; // the scope is myInteger, which is an array myInteger INT[5] {1,2,3,4,5}; // these assignment statements // have no set-value blocks myRecord02.myContent01 = "abc"; myRecord02.myContent02 = "xyz"; // this abbreviated assignment statement // is equivalent to the previous two, and // the scope is myRecord02 myRecord02 { myContent01="abc", myContent02="xyz" }; // This abbreviated assignment statement // resets the first four elements of the array // declared earlier myInteger{6,7,8,9};
缩写赋值语句对固定结构中的字段不可用。
在对字段的字段赋值时,使用这样的语法:其中的 set-value 块在作用域中,所以这些条目仅修改您所关注的字段。
record myBasicRecPart03 type basicRecord myInt04 INT; end record myBasicRecPart02 type basicRecord myInt03 INT; myRec03 myBasicRecPart03; end record myBasicRecPart type basicRecord myInt01 INT; myInt02 INT; myRec02 myBasicRecPart02; end
指定属性值的语法可采用下列适用于字段 myInt04 的三种格式中的任何一种,如以下示例中所示:
// dotted syntax, as described in // References to variables in EGL. myRecB myBasicRecPart { myRec02.myRec03.myInt04{ align = left } }; // bracket syntax, as described in // Bracket syntax for dynamic access. // You cannot use this syntax to affect // fields in fixed structures. myRecC myBasicRecPart { myRec02["myRec03"]["myInt04"]{ align = left } }; // curly-brace syntax myRecA myBasicRecPart { myRec02 {myRec03 { myInt04 { align = left }}} };
// dotted syntax myRecB myBasicRecPart { myInt01 = 4, myInt02 = 5, myRec02.myRec03.myInt04{ align = left }, myRec02.myInt03 = 6 }; // bracket syntax myRecC myBasicRecPart { myInt01 = 4, myInt02 = 5, myRec02["myRec03"]["myInt04"]{ align = left }, myRec02["myInt03"] = 6 }; // curly-brace syntax; // but this usage is much harder to maintain myRecA myBasicRecPart { myInt01 = 4, myInt02 = 5, myRec02 { myRec03 { myInt04 { action = label5 }}, myInt03 = 6 } };
在变量声明或赋值语句中,可以有一个容器(如 SQL 记录)来包括与记录属性同名的字段(keyItems)。要引用字段而不是属性,使用关键字 this,这将为 set-value 块或 set-value 块中的条目建立正确的作用域。
Record myRecordPart type SQLRecord { tableNames = [["myTable"]], keyItems = ["myKey"] } myKey CHAR(10); myOtherKey CHAR(10); keyItems CHAR(60); end
myRecord myRecordPart { keyItems = ["myOtherKey"], this.keyItems = "abc" };
下一节给出数组声明中的另一示例。
col1 ConsoleField[5];
col1 ConsoleField[5] { position = [1,1], color = red };
// assign values to the second and fourth element col1 ConsoleField[5] { this[2] { color = blue }, this[4] { color = blue } };
有关关键字 this 的另一种用法的详细信息,请参阅 EGL 中的确定作用域规则和“this”。
new Menu { labelText = "Universe", MenuItems = // property value is a dynamic array [ new MenuItem { name = "Expand", labelText = "Expand" }, new MenuItem { name = "Collapse", labelText = "Collapse" } ] }
Record Point x, y INT; end Record Rectangle topLeft, bottomRight Point; end
Function test() screen Rectangle { topLeft{x=1, y=1}, bottomRight{x=80, y=24} }; // change x, y in code, using a statement // that is equivalent to the following code: // screen.topLeft.x = 1; // screen.topLeft.y = 2; screen.topLeft{x=1, y=2}; end
pts Point[2] { this[1]{x=1, y=2}, this[2]{x=2, y=3} };
pts{ x=1, y=1 }; pts[1]{x=10, y=20};
在先前示例中,因为数组名有歧义,所以将使用 pts[1] 而不是 this[1]。
points Point[];
points{x=1, y=1};
points[1]{x=10, y=20};
points.resize(2); points{x=1, y=1};