RAD Studio |  |

Working with ADT Fields
Using persistent field components | Using the dataset's FieldByName method | Using the dateset's FieldValues property | Using the ADT field's FieldValues property | Using the ADT field's Fields property | See Also
Collapse All

ADTs are user-defined types created on the server, and are similar to the record type. An ADT can contain most scalar field types, array fields, reference fields, and nested ADTs.

There are a variety of ways to access the data in ADT field types. These are illustrated in the following examples, which assign a child field value to an edit box called CityEdit, and use the following ADT structure,

Copy Code

    Address
      Street
      City
      State
      Zip

Using persistent field components

The easiest way to access ADT field values is to use persistent field components. For the ADT structure above, the following persistent fields can be added to the Customer table using the Fields editor:

Copy Code

    CustomerAddress: TADTField;
    CustomerAddrStreet: TStringField;
    CustomerAddrCity: TStringField;
    CustomerAddrState: TStringField;
    CustomerAddrZip: TStringField;

Given these persistent fields, you can simply access the child fields of an ADT field by name:

Copy Code

    CityEdit.Text := CustomerAddrCity.AsString;

Copy Code

    CityEdit->Text = CustomerAddrCity->AsString;

Although persistent fields are the easiest way to access ADT child fields, it is not possible to use them if the structure of the dataset is not known at design time. When accessing ADT child fields without using persistent fields, you must set the dataset's ObjectView property to True.

Using the dataset's FieldByName method

You can access the children of an ADT field using the dataset's FieldByName method by qualifying the name of the child field with the ADT field's name:

Copy Code

    CityEdit.Text := Customer.FieldByName('Address.City').AsString;

Using the dateset's FieldValues property

You can also use qualified field names with a dataset's FieldValues property:

Copy Code

    CityEdit.Text := Customer['Address.City'];

Copy Code

    CityEdit->Text = Customer->FieldValues["Address.City"];

Note that you can omit the property name (FieldValues) because FieldValues is the dataset's default property.

Note: Unlike other runtime methods for accessing ADT child field values, the FieldValues property works even if the dataset's ObjectView property is False.
Using the ADT field's FieldValues property

You can access the value of a child field with the TADTField's FieldValues property. FieldValues accepts and returns a Variant, so it can handle and convert fields of any type. The index parameter is an integer value that specifies the offset of the field.

Copy Code

    CityEdit.Text := TADTField(Customer.FieldByName('Address')).FieldValues[1];

Copy Code

    CityEdit->Text = ((TADTField*)Customer->FieldByName("Address"))->FieldValues[1];

Because FieldValues is the default property of TADTField, the property name (FieldValues) can be omitted. Thus, the following statement is equivalent to the one above:

Copy Code

    CityEdit.Text := TADTField(Customer.FieldByName('Address'))[1];

Using the ADT field's Fields property

Each ADT field has a Fields property that is analogous to the Fields property of a dataset. Like the Fields property of a dataset, you can use it to access child fields by position:

Copy Code

    CityEdit.Text := TADTField(Customer.FieldByName('Address')).Fields[1].AsString;

Copy Code

    CityEdit->Text = ((TADTField*)Customer->FieldByName("Address"))->Fields->

or by name:

Copy Code

    CityEdit.Text := TADTField(Customer.FieldByName('Address')).Fields.FieldByName('City').AsString;

Copy Code

    CityEdit->Text = ((TADTField*)Customer->FieldByName("Address"))->

Copy Code

    CityEdit->Text = Customer->FieldByName("Address.City")->AsString;

See Also

Working with Array 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!