Programming Guide : Working with Arrays, Table Fields, and Collections : Arrays : How You Can Retrieve Array Information
 
Share this page          
How You Can Retrieve Array Information
It is useful for the control flow of a program to know how many rows are in an array or whether or not all rows have been processed. The ArrayObject class has three attributes that contain quantitative information about an array. Each of these attributes is an integer value that provides some information about the specified array.
These attributes are:
AllRows
Returns the total number of rows in the array, including rows marked deleted. A zero value indicates that there are no rows, not even any marked deleted.
FirstRow
Returns the number of the first row in the array, where first is defined as the row with the lowest number. This attribute is useful for determining whether any rows in the array have been marked deleted.
If this attribute returns zero or a negative number, it indicates that there are rows marked deleted in the array, because rows with a _RowState of RS_DELETED are given non-positive numbers, starting with zero.
For example, assume that an array has six rows, two of which are marked deleted. The array's row numbers are -1, 0, 1, 2, 3, and 4. If you use the FirstRow attribute against this array, it returns the value -1.
If there are no rows having a row state of RS_DELETED, then FirstRow returns 1.
LastRow
Returns the highest non-negative sequence number in the array. (Rows with positive sequence numbers are rows that are not marked deleted.) For example, the value returned by the LastRow attribute on the previously mentioned array is 4. If the value returned by LastRow is zero, there are no undeleted rows in the array, although there may be deleted rows.
These attributes are most often used to control loops in an application, for example:
on click menu.save_menu =
begin
  i = custtable.FirstRow;
  while i <= custtable.LastRow do
      if custtable[i]._rowstate = RS_DELETED then
      ...
      endif;
      i = i +1;
  endwhile;
  commit;
end;
You must be careful if you delete rows in a for loop. The following code is correct:
for i = 1 to array.LastRow do
    if (condition) then
       array.RemoveRow(rownumber = i);
    endif;
endfor;
However, the following code is not correct:
n = array.LastRow;
for i = 1 to n do
    if (condition) then
        array.RemoveRow(rownumber = i);
    endif;
endfor;
In the preceding example, if a row is deleted, there will no longer be n rows in the array. You can remedy this problem by using the downto form of the for statement.
n = array.LastRow;
for i = n downto 1 do
    if (condition) then
        array.RemoveRow(rownumber = i);
    endif;
endfor;