Reputation: 3921
I have a record like this:
Tcustomer=record
Name: string;
IDNumber: Integer;
IsMarried: boolean;
end;
And I have a TCustomers_Manager
class that stores a list of all the customers. Is it possible to have a procedure like this:
Procedure ChangeCustomer(CustomerIndex: integer; field: string; value);
That sets the value for that specific field. For example:
ChangeCustomer(1, 'Name','John');
How can I implement this?
Update: To clarify, my question is basically in 2 parts:
1) How can I map the field name (in string) to the actual field in record?
2) Is it possible to pass a value that has different types? Or should I pass a single type and type cast it (like passing a string and then using strtoint()
)
Upvotes: 0
Views: 1594
Reputation: 57583
You could, for example, do this (assuming lst: TList<TCustomer>
as you said in a comment):
Procedure ChangeCustomer(index: integer; i: byte; value: variant)
begin
case (i) of
0: lst[index].Name := value;
1: lst[index].IDNumber := value;
2: lst[index].IsMarried := value;
end;
end;
You could use a type (or enum) in place of i: byte
.
I dont't use Delphi from long time, so take my example as an idea, not like a Delphi app!!
Upvotes: 1
Reputation: 84
I would use TClientDataSet.
Or you can take any MemoryDataSet component and use it in same way.
Second way is to convert record to class, declare Fields as published and use SetPropValue.
If you want to pass any Value you can use Variant. But you have to check types before assign.
Upvotes: 3