6. Working with Arrays, Table Fields, and Collections : Arrays : How You Can Manipulate Arrays : How You Can Add Rows to an Array
 
Share this page                  
How You Can Add Rows to an Array
Because creating an array does not populate it, adding rows to the array may be the first task to perform after declaring the array. There are several ways that you can populate an empty array or add additional rows to a populated array:
Add rows by first assignment
Use the InsertRow method
Assign the contents of one array to another
Create a populated array by duplicating an existing array
Allow the user to append to a table field
The following sections describe each of these methods in more detail.
Add Rows by First Assignment
When you make an assignment to a nonexistent row in your 4GL code, OpenROAD creates that row if it is the next row in the array sequence; if it is not, an error occurs. This is an easy way to fill an array.
For example, the following select loop fills the custacct array:
i = 1;
select :custacct[i].acctno = cacctno,
       :custacct[i].custname = cname,
       :custacct[i].acctbalance = acctbal
from customer
begin
    i = i + 1;
end;
These statements perform the following operations:
1. Place the first row that the select returns into the first row of the array.
2. For each row retrieved, increment i (the index into the array) by one.
3. Place the second returned row into the second row in the array (in the second iteration of the loop) and again increment i.
4. Continue this process for each row returned by the select statement.
If any of the array's rows were nonexistent prior to executing the select statement, they are automatically created when the statement is executed.
This method only allows rows to be added to the end of an array or to an empty array. Also, the index must be incremented by one; this method does not work if row numbers are skipped.
When you use this method to add rows to an array that is displayed in a table field, OpenROAD updates the display when the event block containing the select loop completes.
Use the InsertRow Method of the ArrayObject Class
The syntax for this method is:
integer = arrayname.InsertRow([rownumber = integer]
          [, rowobject = object] [,_rowstate = integer]);
arrayname
Specifies the name of the array
object
Specifies a reference variable that points to an object that is either of the same class as, or is a subclass of, the array's class. When the statement is executed, OpenROAD inserts the object into the array at the specified position.
If you do not specify an object, OpenROAD constructs an object whose attributes contain default values and inserts the object into the array. Default values are null for nullable fields, zero for numeric fields and blanks for character fields.
Another way to set default values for a column is by setting default values for the ProtoField of the column. For more information, see TableField, ColumnField, and ProtoField Objects (see TableField, ColumnField, and ProtoField Objects).
The InsertRow method inserts the new row ahead of the row specified by the rownumber parameter and adjusts the numbers of the rows following the new row. For example, if you insert a row into the third row position in the array, the current row 3 becomes row 4. If you do not specify the rownumber parameter, OpenROAD inserts the new row as row 1. To insert a row at the end of the array, set rownumber = arrayname.LastRow+1.
By setting _rowstate, you can set the _rowstate to other than the default RS_CHANGED.
Inserting a default object is the best way to insert an empty row into an array. For example, the following code inserts a blank row at the specified position:
custtable.InsertRow(rownumber = row);
The inserted row is blank because the rowobject parameter is not specified. This technique is especially useful if you display an array in a table field and want the user to enter new data in a particular position in the array.
If you set rowobject to null, 4GL creates the row number in the array, but no object is associated with the new row. This feature provides useful program-control capabilities but should not be used if the array is displayed by a table field.
Assign the Contents of One Array to Another Array
Assume that you have an array variable, custtable, that contains customer addresses and an empty array variable, addresses. The following statement assigns the contents of custtable to addresses:
addresses = custtable;
This statement does not create two distinct objects, but rather two array variables that point to the same set of objects, that is, to the same array. Both custtable and addresses now reference the same array.
Create a Populated Array by Duplicating an Existing Array
If you want two separate array variables so that you can modify the data in one array without affecting the other, you can create a second copy of the array by using the Duplicate method defined for the Object system class.
You can make an exact duplicate of an array, placing a reference to the new array in a new reference variable, provided the new reference variable is of the same class, or a superclass of, the array being copied.
The following example creates a new array object called array2 which is an exact duplicate of array1:
declare
  array1 = array of myclass;
  array2 = array of myclass;
enddeclare
  begin
      /* load values into array1 */
      /* then duplicate array1 */
      array2 = array1.Duplicate();
      ...
Allow the User to Append to a Table Field
A table field may or may not allow the user to add new rows at the bottom of the array. For more information about this, see How You Can Append Rows to End of Table Field (see How You Can Append Rows to the End of a Table Field).