# Custom Record/Structure Types

TRecord This class allows you to simulate a Pascal record type. You can create an object of this class with any number of fields, and get and set the values of these fields.
Function/Property | Description
CreateFields(Fields: string): TRecord | Creates a TRecord object with the specified Fields.
AddFields(Fields:string) | Adds the specified fields to the record object
GetName(Index: Integer): string | Gets the name of the Field at the specified Index.
GetValue(IndexOrName: Variant): Variant | Gets the value of a Field given an Index or field Name.
SetName(Index: Integer; Name: string) | Sets the name of the Field at the specified Index.
SetValue(IndexOrName: Variant; Value: Variant) | Sets the value of a Field given an Index or field Name.
Value[IndexOrName: Variant]: Variant | Gets/Sets the value of a Field given an Index or field Name. Note, this is the "default" property for a TRecord object so you do not need to use the "Value" part.
AsText: string | Returns all the fields and values of a TRecord object.
Getting and Setting TRecord field values Example: var r: TRecord; begin // Create a record with 2 fields r := TRecord.CreateFields('FirstName,Surname'); // Set the field values - you can use [] or GetValue + SetValue methods to access the fields. r['FirstName'] := 'John'; r.SetValue('Surname', 'Smith'); ShowMessage(r.GetValue('FirstName')); r.SetValue('FirstName', 'Mike'); // AsText will output each field: value pair. Output.Add(r.AsText); end; Creating Sub-records A field in a TRecord can also be a TRecord itself. To specify that a field is a sub-record, use '#' as the first character in its name. Example: var person: TRecord; address: TRecord; begin // Using the # character means that the record's field will actually refer to another record. person := TRecord.CreateFields('FirstName,Surname,Age,#Address'); person['FirstName'] := 'John'; person['Surname'] := 'Smith'; person['Age'] := 21; // To access a record within a record you need to use another TRecord. Note you do not need to "create" a TRecord, as it will already be instantiated for you. address := person['Address']; address.AddFields('Address1,Address2,City,State'); // You can modify address's fields via the address "pointer", or by using a dot (.) within the person pointer. address['Address1'] := 'Apartment 999'; person['Address.Address2'] := '999 Main Street'; Output.Add(person.AsText); end;