3

how can i get the record field value? for example,

sorry , my Description is not clear, i have a big record like this

type
myRec=record
a:byte;
c:single;
////  a lot of  fields
end;
var
nowmyrec:myrec;
tmystr:TMemoryStream;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
tmystr:tmemerystream.create;
tmystr.LoadFromFile(ExtractFilePath(Application.exename)+'1.data');
tmstr.Position:=0;
tmstr.readbuffer(nowmyRec,sizeof(myRec));
end;

if fields of nowmyRec is 1000, how can i get the dll of field value to 1000 form.edits , please donot use record point ,i want use rtti of record,but delphi2007 donot support that. if i donot use delphi2010 ,do you have other ways?

Blockquote

babaloveyou
  • 229
  • 2
  • 8
  • I edited out the unreferenced array field and added in the missing `c` in the final line of code – David Heffernan Feb 19 '11 at 09:54
  • 1
    Please edit the question so that the code is formatted properly (Indent by 4 spaces). Please think about what your question is and take some time to explain it carefully. As it stands two people have answered a question totally unrelated to you actual problem. That was caused by you asking the question badly. – David Heffernan Feb 19 '11 at 10:35
  • You can't call `TStream.ReadBuffer`, you need an instance. Your question is still not clear. Unless you can ask it clearly I will vote to close. If you are going to format code put some indentation and lin breaks in. Do you really write code without indentation? Why do you expect us to read it like that - it's makes my head hurt. – David Heffernan Feb 19 '11 at 10:47
  • 1
    I give up. Each edit and comment makes the question less clear than before. Vote to close. Sorry. – David Heffernan Feb 19 '11 at 11:50
  • i change my code .help again – babaloveyou Feb 19 '11 at 11:55
  • if you must use rtti on a record then you need a version of delphi that supports it. Or use a dictionary. – David Heffernan Feb 19 '11 at 12:13
  • What is a "dll of field"? You want to create a form with 1000 edits one edit for each field in the record? – Mikael Eriksson Feb 19 '11 at 12:29
  • 2
    one question at a time please. Mikael and bharat answered your first question. I answered the edited question about rtti. Please accept one of these and ask a new question. But spend more time writing formatting and explaining. – David Heffernan Feb 19 '11 at 13:54
  • 2
    @babaloveyou Welcome to StackOverflow. I downvoted your question not because the English is bad (mine is bad too), but because the source code sample is unreadable and won't compile as is. It clearly shows you are not putting energy into asking your question. Pay attention to naming of your fields, variables, indenting, consistent uppercase/lowercase and other good habits. Also read the FAQ: http://stackoverflow.com/faq That tremendously helps the volunteers at StackOverflow answering your question. Others voted for closing the question, but I'd like to give you a chance of improving it. – Jeroen Wiert Pluimers Feb 19 '11 at 14:06
  • 2
    @Jeroen We have already tried to help the Q to be improved. If it is edited further it will be onto the 3rd different question. It's not fair to those that have answers that end up looking utterly stupid because they are answers to a different question that has long gone. Better would be to close and have a new question. As it stands the two questions asked so far have been answered well and these answers deserve recognition. – David Heffernan Feb 19 '11 at 14:38

3 Answers3

3

You question is "how do I use RTTI to access record fields in Delphi 2007?" The answer is that you can't, you need Delphi 2010.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

You can declare the record like this

type
    TmyRec = record
        a: byte;
        b: array [0 .. 35] of widechar;
        c: single;
    end;

And use it like this

var
    myRec1, MyRec2: TmyRec;
    ms: TMemoryStream;
    x: single;
begin
    ms := TMemoryStream.Create;
    try
        // Create a record
        myRec1.a :=1;
        myRec1.c :=1.50;

        // Save record to TMemoryStream
        ms.WriteBuffer(myRec1, SizeOf(TmyRec));

        // Read one record from TMemoryStream
        ms.Position := 0;
        ms.ReadBuffer(myRec2, SizeOf(TMyRec));

        // Get a value
        x := MyRec2.c;
        ShowMessage(FloatToStr(x));
    finally
        ms.Free;
    end;
end;
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
  • i mean i want to get myRec.a value and myRec.b value, not Assignment, – babaloveyou Feb 19 '11 at 10:05
  • @baba: New version that store a record in a stream and reads it back again to use a value. – Mikael Eriksson Feb 19 '11 at 10:49
  • if my record have 1000 fields , i must wirte 1000 lines x := MyRec2.c; that i will dead – babaloveyou Feb 19 '11 at 10:52
  • @baba: What do you want to do with the fields? – Mikael Eriksson Feb 19 '11 at 10:55
  • 1
    I'd also point out that such streaming will only work for value types (not strings, dynamic arrays etc.). It won't readily admit changes to record structure as the program develops. But if you don't tell us what you are doing it's very hard to guess. – David Heffernan Feb 19 '11 at 10:59
  • @David: Good point. The "write to stream" part of this was only to have something to show that works. Not a recommendation on how to build database of your own :). – Mikael Eriksson Feb 19 '11 at 11:08
  • i want to read a big array record ,but the record have a lot of fields. i can not show this fields – babaloveyou Feb 19 '11 at 11:10
  • @Mikael I totally understand where you are coming from. I want to up-vote you more for your efforts in the face of total lack of information! – David Heffernan Feb 19 '11 at 11:10
  • http://stackoverflow.com/questions/3164276/get-the-offset-of-a-field-in-a-delphi-record-at-runtime my example look like this but delphi2007 not Support the reord rtti – babaloveyou Feb 19 '11 at 11:17
  • @baba: You only have to key in the fields that you actually are going to use for something. If you somehow need to use 1000 individual fields from a record you have to type it 2000 times. 1000 times in the record definition and 1000 times where you use it. – Mikael Eriksson Feb 19 '11 at 11:20
  • 2
    @baba: I think you should delete this question and ask a new one that actually describes what you are trying to do. Also add the link to the other questions and explain why that is not working for you. So far in this question you have only described what you do not want to do and that is typing a lot of field names. – Mikael Eriksson Feb 19 '11 at 11:29
  • i read a game database ,the database Structure is array record, Not my decision – babaloveyou Feb 19 '11 at 11:32
  • what is an array record? Which of these is it. It can't be both. Or is it an array of records? – David Heffernan Feb 19 '11 at 12:17
  • it is array of records ,sorry, my english is weak – babaloveyou Feb 19 '11 at 12:39
  • @baba - If your question is similar to the one you refer, your answer is similar too. Delphi 2007 and Delphi 7 are not different in this respect. – Sertac Akyuz Feb 19 '11 at 12:51
  • but i do not want use delphi2010 do you have a other way? – babaloveyou Feb 19 '11 at 14:06
  • @sertac the answers to that question are that d7 can't do it either! – David Heffernan Feb 19 '11 at 14:13
  • @David - Yup, I meant the answer for D2007 is similar to the answer for D7. – Sertac Akyuz Feb 19 '11 at 14:47
  • @David - Heh, there's a lot of confusion on this page. – Sertac Akyuz Feb 19 '11 at 14:54
  • sorry everyone, this is my first question in stackoverflow, english is not my native language. – babaloveyou Feb 20 '11 at 06:43
1

you need to first declare the record variable like Rec1: myRec;

var
  Rec1: myRec;
  myValue: Single;
begin
  myValue := Rec1.c
end;

Is this what you want?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Bharat
  • 6,828
  • 5
  • 35
  • 56