Search code examples
delphidelphi-7rtti

Is there a way to update a field in a record knowing the field name and value


Given a Record:

MyRecord = record
    Company: string;
    Address: string;
    NumberOfEmplyees: integer;

can you write a function call like

function UpdateField(var FieldName: string; FieldValue: variant): bool;

so that:

UpdateField('Company', 'ABC Co');

would update MyRecord.Company to 'ABC Co'?

I looked for an example but everything I found is for a database. Any help pointing me in the right direction is appreciated.

Thanks, Charles


Solution

  • You need modern versions of Delphi to do what you ask for without resorting to manually coding the lookups, e.g. via a table.

    The updated RTTI introduced in Delphi 2010 can support what you are looking for, but there's nothing in Delphi 7 that will do this for records.