Programming Guide : Creating Dynamic Frames : How You Can Manipulate Data in Dynamic Fields : How You Can Access Individual Cells of a Dynamic Table Field
 
Share this page          
How You Can Access Individual Cells of a Dynamic Table Field
You cannot use the GetFieldValue and SetFieldValue methods to obtain or change data values in cells in a dynamic table field. Use the following procedure to access the values in an array underlying a dynamic table field
To access the values in an array underlying a dynamic table field, you would perform the following basic steps:
1. Create a dynamic expression—an object of the DynExpr system class that represents an expression that can be reevaluated dynamically or a variable that can be reevaluated or assigned dynamically.
2. Use one of the following methods, defined for the DynExpr system class, to get or set values:
Assign
Assigns the value of one dynamic expression to another
GetValue
Puts the value of the field referenced by the dynamic expression into a variable
SetValue
Puts the value of an expression into the field referenced by the dynamic expression
How You Can Create a Dynamic Expression
You create DynExpr objects by invoking the CreateDynExpr method on an object of the Scope class. The Scope object represents the context for evaluation of the DynExpr object. The dynamic expression is no longer valid when its scope terminates, that is, when its associated application, frame, or other component, closes.
The CreateDynExpr method takes the following parameters:
string
Contains any syntactically valid OpenROAD expression except procedure or method invocations. Use the string parameter to specify the field or table field cell whose value you want to access. This parameter (which takes varchar values) can reference the field directly or can contain a valid OpenROAD expression that references the field.
The value of the string parameter is loaded into the dynamic expression created by the CreateDynExpr method. You can then use the dynamic expression to access the value of the field or cell originally specified.
For example, the following statement creates a dynamic expression (dexp) that contains the value of a cell in the Salary column of a table field named tbl:
declare
    dexp = DynExpr default null;
enddeclare
dexp = CurFrame.Scope.CreateDynExpr(string =
    'tbl[i].Salary');
When the dynamic expression is used in a GetValue, SetValue, or Assign method invocation, the value of i must be a valid row number for the array.
The value of the string parameter is evaluated within the context of the named scope. In the previous statement, the dexp dynamic expression is valid while the current frame is running. The scope in the following statement limits the dynamic expression to the current event block:
declare
    dexp = DynExpr default null;
enddeclare
dexp = CurEventScope.CreateDynExpr(string =
    'tbl[i].Salary');
Any attempt to use the dynamic expression after the event block terminates causes a runtime error.
datatype
Specifies that the datatype of the compiled expression should be returned. The returned datatype includes nullability and is in script declaration format, for example:
varchar(32) not null
stringobject
integer2
myuserclass
This enables the datatype of variables declared in the script—and system, global, field, and attribute variables—to be determined at runtime. For example, after the following statement:
mydynexpr = curframe.Scope.CreateDynExpr(string='curframe', datatype=Byref(datatype));
the value of datatype is:
frameexec
Note:  The returned datatype name may be a synonym of the declared datatype, rather than the matching name; for example, a declared datatype of "smallint" will be returned as "integer2".
errors
(Optional.) Provides the text of the error message. If the scope object on which the CreateDynExpr method is invoked is invalid, or the text specified in the string parameter cannot be compiled, the CreateDynExpr method returns a null.
If compilation errors occur, the CreateDynExpr method creates a string object containing the errors and sets the variable specified by the errors parameter (of StringObject data type) to point to that string object. If no compilation errors occur, this parameter is set to null.
The following statement creates a dynamic expression and sets the errors parameter:
declare
    dexp = DynExpr default null;
enddeclare
dexp = CurFrame.Scope.CreateDynExpr(string =
    'tbl[i].Salary', errors = byref(stringvar);
If an error occurs creating the dexp dynamic expression, the stringvar variable contains the error message in its Value attribute.
How You Can Set and Get Values with Dynamic Expressions
You can use the following methods to get and set values in the field referenced by a dynamic expression:
GetValue to obtain the value from the field
SetValue and Assign to change values in the field
The GetValue method reevaluates the expression that was specified when the DynExpr object was created and places the value of the dynamic expression in the specified variable.
For example, to get the value of a dynamic expression that was created for a field whose data type is money, create a money variable and invoke DynExpr's GetValue method to get the value from the dynamic expression and load it into the money variable.
The following statement performs this operation:
status = dexp.GetValue(value = byref(money_var));
If the GetValue method succeeds, status is set to ER_OK; if it fails, it is set to some other integer value.
Setting a value in a dynamic expression works similarly. Use DynExpr's SetValue method to load a value from a valid OpenROAD expression into a dynamic expression.
The following code sets the value of the dynamic expression from a variable parameter to the SetValue method:
status = dexp.SetValue(value = money_var);
Like the GetValue method, the SetValue method sets status to ER_OK if successful; if it fails, it sets status to some other integer value.
The SetValue method is valid only if the expression specified when the dynamic expression was created was not a computed expression. The GetValue method is valid only if the expression specified when the dynamic expression was created represents a field or table field cell.
You can get a value from a dynamic expression and assign a new one in a single step using the Assign method. For an example of using the Assign method, see How You Can Get Values from Unknown Fields.
How You Can Get Values from Unknown Fields
The previous examples of getting and setting values with dynamic expressions assume you know the name and data type of the current table field column. Assume instead that you want to get a value from the current cell of a dynamic table field named tbl but you do not know which cell currently has the input focus. Assume also that all columns in the table have a data type of integer or varchar.
When you invoke the GetValue method, you must put the value obtained into a variable of the appropriate data type. Therefore, getting the value from the current cell requires using a conditional statement and creating a dynamic expression and invoking the GetValue method.
The following field script performs these operations when the user enters a cell in a table field:
declare
    txt = varchar(50) not null;
    dexp = DynExpr default null;
    errstr = StringObject default null;
    intvar = integer not null;
    textvar = varchar(50) not null;
    status = integer not null;
enddeclare
on childentry =
begin
    txt = 'tbl[].' + CurFrame.TriggerField.Name;
    dexp = CurFrame.Scope.CreateDynExpr
        (string = txt, errors = byref(errstr));
    if CurFrame.TriggerField.DataType = 'integer'
        then status = dexp.GetValue(value =
                byref(intvar));
    else
        status = dexp.GetValue
                (value = byref(textvar));
    endif;
    ...
end
After declaring the appropriate variables, this code uses the Name attribute defined for the FieldObject system class to create a text string that contains the full name of the current table field cell.
If the expression in the txt variable is valid (that is, points to an existing field and performs valid operations), the errstr variable is null after the statement creating this dynamic expression; otherwise, it contains the text of the error message.
The following code assigns a new value to the same cell in tbl, using the same dynamic expression:
intvar = intvar * 1.15;
status = dexp.SetValue(value = intvar);
You can get a value from the cell and assign a new one in a single step using the Assign method. Because the Assign method assigns the value of one dynamic expression to another, you can use this method to copy values between dynamic expressions without knowing the data type of the value in each expression.
For example, the following code creates two dynamic expressions, one containing the current value of the specified cell and the other containing that value increased by 15 percent, and then assigns the value of the second dynamic expression to the first:
txt = 'tbl[i].' + CurFrame.TriggerField.Name;
dexp_a = CurFrame.Scope.CreateDynExpr(string = txt,
    errors = byref(errstr));
txt = txt + '* 1.15';
dexp_b = CurFrame.Scope.CreateDynExpr(string = txt,
    errors = byref(errstr));
status = dexp_a.Assign(fromdynexpr = dexp_b);
Although using the Assign method in this manner requires creating two dynamic expressions, it eliminates the need for an intermediate variable.
For more information, see How You Can Create a Dynamic Expression.