给定一个记录类型:
TItem = record
UPC : string[20];
Price : Currency;
Cost : Currency;
...
end;
如何获取一个字段的偏移量(以字符串形式给出字段名)?我需要在运行时进行此操作 - 要访问的字段名称在运行时决定。
示例:
var
pc : Integer;
fieldName : string;
value : Currency;
begin
pc := Integer(@item); // item is defined and filled elsewhere
fieldName := Text1.Text; // user might type 'Cost' or 'Price' etc
Inc(pc, GetItemFieldOffset(fieldName)); // how do I implement GetItemFieldOffset?
value := PCurrency(pc)^;
..
我正在使用Delphi 7。