Using persistent fields

by Mark Cashman

Persistent fields are, of course, useful for lookup and calculated fields, but you can also use them to avoid references such as:

S = Account->Status->
  FieldByName("DESCRIPTION")->AsString;

This type of reference might be vulnerable to a DBMS change and is best avoided. Using persistent fields allows you to instead to use code like this:

S = Account->Status->
  StatusDescription->AsString; 

Note that even if you rename the field object from its default, it must have a name unique in the data module. Otherwise we could refer to

S = Account->Status->Description->AsString; 

Adding the table name to the field name is probably the least objectionable way to deal with this problem.

There are other important factors in the use of persistent fields. First, use of persistent fields is independent of the sequence of fields in the table. As a result, reordering those fields has no effect on your use of the fields (though renaming and deleting does). Second, you can control the display of a table field in a grid or elsewhere by setting the Visible property of the field (which controls whether of not the field is shown in a grid), its EditMask, its Alignment, its DisplayWidth, and its DisplayLabel. Third, you can attach events to the field to deal with new or changed content (in the OnSetText and OnValidate event handlers, for example). Fourth, references through persistent fields are very efficient compared to FieldByName, since a lookup of the field name and determination of its status and position in the row does not need to be done on each reference to the field.