RAD Studio
ContentsIndex
PreviousUpNext
Using the Eof and Bof Properties

Two read-only, runtime properties, Eof (End-of-file) and Bof(Beginning-of-file), are useful when you want to iterate through all records in a dataset.

When EOF is True, it indicates that the cursor is unequivocally at the last row in a dataset. Eof is set to True when an application

  • Opens an empty dataset.
  • Calls a dataset's Last method.
  • Calls a dataset's Next method, and the method fails (because the cursor is currently at the last row in the dataset.
  • Calls SetRange on an empty range or dataset.
Eof is set to False in all other cases; you should assume Eof is False unless one of the conditions above is met and you test the property directly. 

Eof is commonly tested in a loop condition to control iterative processing of all records in a dataset. If you open a dataset containing records (or you call First) Eof is False. To iterate through the dataset a record at a time, create a loop that steps through each record by calling Next, and terminates when Eof is True. Eof remains False until you call Next when the cursor is already on the last record. 

The following code illustrates one way you might code a record-processing loop for a dataset called CustTable:

CustTable.DisableControls;
try
  CustTable.First; { Go to first record, which sets Eof False }
  while not CustTable.Eof do { Cycle until Eof is True }
  begin
    { Process each record here }
    .
    .
    .
    CustTable.Next; { Eof False on success; Eof True when Next fails on last record }
  end;
finally
  CustTable.EnableControls;
end;

 

CustTable->DisableControls(); // Speed up processing; prevent screen flicker
try
{
  while (!CustTable->Bof) // Cycle until Bof is true
  (
    // Process each record here
    .
    .
    .
    CustTable->Prior();
    // Bof false on success; Bof true when Prior fails on first record
  }
}
__finally
{
  CustTable->EnableControls();
}

 

CustTable->DisableControls();
try
{
  for (CustTable->First(); !CustTable->Eof; CustTable->Next())
  (
    // Process each record here
    .
    .
    .
  }
}
__finally
{
  CustTable->EnableControls();
}

Tip: This example also shows how to disable and enable data-aware visual controls tied to a dataset. If you disable visual controls during dataset iteration, it speeds processing because your application does not need to update the contents of the controls as the current record changes. After iteration is complete, controls should be enabled again to update them with values for the new current row. Note that enabling of the visual controls takes place in the finally clause of a try...finally statement. This guarantees that even if an exception terminates loop processing prematurely, controls are not left disabled.

When BOF is True, it indicates that the cursor is unequivocally at the first row in a dataset. Bof is set to True when an application

  • Opens a dataset.
  • Calls a dataset's First method.
  • Calls a dataset's Prior method, and the method fails (because the cursor is currently at the first row in the dataset.
  • Calls SetRange on an empty range or dataset.
Bof is set to False in all other cases; you should assume Bof is False unless one of the conditions above is met and you test the property directly. 

Like EOF, Bof can be in a loop condition to control iterative processing of records in a dataset. The following code illustrates one way you might code a record-processing loop for a dataset called CustTable:

CustTable.DisableControls; { Speed up processing; prevent screen flicker }
try
  while not CustTable.Bof do { Cycle until Bof is True }
  begin
    { Process each record here }
    .
    .
    .
    CustTable.Prior; { Bof False on success; Bof True when Prior fails on first record }
  end;
finally
  CustTable.EnableControls; { Display new current row in controls }
end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!