English 中文(简体)
• 如何在2010年德尔菲斯挖掘一个记录领域?
原标题:How to Traverse an fields of Record in delphi2010?
  • 时间:2011-02-21 16:03:06
  •  标签:
  • delphi

I want to read fields of a record to a string list or an array of edit controls. My code is error, please fix it; and how to Write traversal the fields.

type
  TItem = record
   a : string[20];
   b : word;
   c  : word;
  end;
var
  Form1: TForm1;

implementation
 uses rtti;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  rttiContext: TRttiContext;
  rttiType: TRttiType;
  fields: TArray<TRttiField>;
  item: TItem;
  i:word;
begin
  item.a:= hello ;
  item.b:=123;
  item.c:=456;
  rttiType := rttiContext.GetType(TypeInfo(TItem));
  fields := rttiType.GetFields;
   for i := low(fields) to high(fields) do
   begin
    ShowMessage(fields[i].GetValue(@item).AsString);
   end;

end;

end.
最佳回答

I had to change string[20] to string (I do not know why and I do not know how to get it to work with string[20]). And change AsString to ToString. This is tested on Delphi XE and Delphi 2010.

type
    TItem = record
     a : string;
     b : word;
     c  : word;
    end;

procedure TForm3.FormCreate(Sender: TObject);
var
    rttiContext: TRttiContext;
    rttiType: TRttiType;
    fields: TArray<TRttiField>;
    item: TItem;
    i:word;
begin
    item.a:= hello ;
    item.b:=123;
    item.c:=456;
    rttiType := rttiContext.GetType(TypeInfo(TItem));
    fields := rttiType.GetFields;
     for i := low(fields) to high(fields) do
     begin
        ShowMessage(fields[i].GetValue(@item).ToString);
     end;
end;
问题回答

暂无回答




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签