RAD Studio |  |

Working with Array Fields
Using persistent fields | Using the array field's FieldValues property | Using the array field's Fields property | See Also
Collapse All

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.

Using persistent fields

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:

Copy Code

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

Copy Code

    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.

Copy Code

    TelEdit.Text := CustomerTelNos_Array0.AsString;

Copy Code

    TelEdit->Text = CustomerTELNOS_ARRAY0->AsString;

Using the array field's FieldValues property

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,

Copy Code

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

Copy Code

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

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

Copy Code

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

Using the array field's Fields property

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:

Copy Code

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

Copy Code

    for (int i = 0; i < OrderDates->Size; ++i)
      if (!OrderDates->Fields->Fields[i]->IsNull)
        OrderDateListBox->Items->Add(OrderDates->Fields->Fields[i]->AsString);

See Also

Working with ADT Fields

Working with DataSet Fields

Working with Reference Fields

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!