set-value 块

set-value 块是一个代码区域,可在其中设置属性和字段值。有关背景,请参阅 EGL 属性概述

set-value 块在您采取下列任何操作时可用:

在后两种情况下,仅对字段赋值。

注: 固定结构中的字段存在限制。可使用 set-value 块来指定基本字段级别属性的值,但不能使用它设置字段本身的值。

基本情况的 set-value 块

考虑大多数基本情况下适用的规则:
  • 每个 set-value 块以左花括号({)开始(包括用逗号隔开的一列条目或单个条目)并以右花括号(})结束
  • 这些条目全部使用以下两种格式的其中之一:
    • 每个条目由标识-值对组成,如 inputRequired = yes;或者
    • 每个条目包含按位置指定的值,即将连续值赋给了一个数组的连续元素。

在所有情况下,set-value 块在要修改的部件、变量或字段的作用域中。下面用示例对语法中的变化作最好的演示。

第一个示例显示 dataItem 部件,它有两个属性(inputRequiredalign):
  // 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
   };
下一个示例显示了 SQL 记录部件声明,它包括两个记录属性(tableNameskeyItems):
  // 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 块

在对字段的字段赋值时,使用这样的语法:其中的 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
可对任何字段指定属性值,如下所示:
  • 为该记录创建 set-value 块
  • 嵌入一系列字段名以缩小作用域
  • 创建特定于字段的 set-value 块

指定属性值的语法可采用下列适用于字段 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 }}}
  };
甚至在复杂情况下,您也可以使用逗号在 set-value 块中分开各个条目;但您需要考虑嵌套给定块的级别:
  // 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
      }
  };

“this”的用法

在变量声明或赋值语句中,可以有一个容器(如 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
    
以下记录声明首先为属性 keyItems 设置值,然后为同名字段设置值:
  myRecord myRecordPart 
  { 
     keyItems = ["myOtherKey"],
     this.keyItems = "abc"
  };

下一节给出数组声明中的另一示例。

set-value 块、数组和数组元素

在声明动态数组时,可指定元素的初始数目,如以下示例中所示:
  col1 ConsoleField[5];
set-value 块中的赋值引用类型为 ConsoleField 的每一个初始元素中的属性和预定义字段(可是并非引用以后添加的任何元素):
  col1 ConsoleField[5]
  { 
    position = [1,1],
    color = red
  };
要对变量声明中的特定元素赋值,创建作用域为该元素的嵌入式 set-value 块。如以下示例中所示,通过将关键字 this 与带方括号的下标来指定作用域:
  // assign values to the second and fourth element
  col1 ConsoleField[5]
  {
    this[2] { color = blue }, 
    this[4] { color = blue }
  };

有关关键字 this 的另一种用法的详细信息,请参阅 EGL 中的确定作用域规则和“this”

可在 set-value 块中的位置条目来对以下任何类型的数组中的连续元素赋值(仅在处理报告或创建控制台表单时才起作用):
  • ConsoleField
  • 菜单
  • MenuItem
  • 提示
  • 报告
  • ReportData
以下示例可以在 OpenUI 语句中。每个嵌入式 set-value 块的作用域是一个特定数组元素:
   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
接下来,在同一函数中初始化类型为 Point 的动态元素数组:
  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]。

接下来,考虑类型为 Point 的另一个动态数组:
  points Point[];
因为不存在任何元素,所以以下赋值语句不起作用:
  points{x=1, y=1};
相比之下,因为引用了特定元素并且该元素不存在,所以以下赋值语句将导致越界异常:
  points[1]{x=10, y=20};
可将元素添加至数组,然后使用单个语句设置所有元素的值:
  points.resize(2);
  points{x=1, y=1};
使用条款 | 反馈
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.