RAD Studio
ContentsIndex
PreviousUpNext
Working with Array Fields

Array fields consist of a set of fields of the same type. The field types can be scalar (for example, float, string), or non-scalar (an ADT), but an array field of arrays is not permitted. The SparseArrays property of TDataSet determines whether a unique TField object is created for each element of the array field. 

There are a variety of ways to access the data in array field types. If you are not using persistent fields, the dataset's ObjectView property must be set to True before you can access the elements of an array field.

You can map persistent fields to the individual array elements in an array field. For example, consider an array field TelNos_Array, which is a six element array of strings. The following persistent fields created for the Customer table component represent the TelNos_Array field and its six elements:

CustomerTelNos_Array: TArrayField;
CustomerTelNos_Array0: TStringField;
CustomerTelNos_Array1: TStringField;
CustomerTelNos_Array2: TStringField;
CustomerTelNos_Array3: TStringField;
CustomerTelNos_Array4: TStringField;
CustomerTelNos_Array5: TStringField;

 

CustomerTELNOS_ARRAY: TArrayField;
CustomerTELNOS_ARRAY0: TStringField;
CustomerTELNOS_ARRAY1: TStringField;
CustomerTELNOS_ARRAY2: TStringField;
CustomerTELNOS_ARRAY3: TStringField;
CustomerTELNOS_ARRAY4: TStringField;
CustomerTELNOS_ARRAY5: TStringField;

Given these persistent fields, the following code uses a persistent field to assign an array element value to an edit box named TelEdit.

TelEdit.Text := CustomerTelNos_Array0.AsString;

 

TelEdit->Text = CustomerTELNOS_ARRAY0->AsString;

You can access the value of a child field with the array field's FieldValues property. FieldValues accepts and returns a Variant, so it can handle and convert child fields of any type. For example,

TelEdit.Text := TArrayField(Customer.FieldByName('TelNos_Array')).FieldValues[1];

 

TelEdit->Text = ((TArrayField*)Customer->FieldByName("TelNos_Array"))->FieldValues[1];

Because FieldValues is the default property of TArrayField, this can also be written

TelEdit.Text := TArrayField(Customer.FieldByName('TelNos_Array'))[1];

TArrayField has a Fields property that you can use to access individual sub-fields. This is illustrated below, where an array field (OrderDates) is used to populate a list box with all non-null array elements:

for I := 0 to OrderDates.Size - 1 do
begin
  if not OrderDates.Fields[I].IsNull then
    OrderDateListBox.Items.Add(OrderDates[I]);
end;

 

for (int i = 0; i < OrderDates->Size; ++i)
  if (!OrderDates->Fields->Fields[i]->IsNull)
    OrderDateListBox->Items->Add(OrderDates->Fields->Fields[i]->AsString);
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!