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,
Address Street City State Zip
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:
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:
CityEdit.Text := CustomerAddrCity.AsString;
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.
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:
CityEdit.Text := Customer.FieldByName('Address.City').AsString;
You can also use qualified field names with a dataset's FieldValues property:
CityEdit.Text := Customer['Address.City'];
CityEdit->Text = Customer->FieldValues["Address.City"];
Note that you can omit the property name (FieldValues) because FieldValues is the dataset's default 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.
CityEdit.Text := TADTField(Customer.FieldByName('Address')).FieldValues[1];
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:
CityEdit.Text := TADTField(Customer.FieldByName('Address'))[1];
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:
CityEdit.Text := TADTField(Customer.FieldByName('Address')).Fields[1].AsString;
CityEdit->Text = ((TADTField*)Customer->FieldByName("Address"))->Fields->
or by name:
CityEdit.Text := TADTField(Customer.FieldByName('Address')).Fields.FieldByName('City').AsString;
CityEdit->Text = ((TADTField*)Customer->FieldByName("Address"))->
CityEdit->Text = Customer->FieldByName("Address.City")->AsString;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|