Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help using record types. I need to use a memo field:) Any help?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Look at this code:

type
house=record
id:integer;
description:string[60];
m2:integer;
obs:memo; {This is the wrong part}
end;

var
Thouse:house;

assignfile (file1,'teste.txt');
reset (file1,1);
Thouse.id:=1;
Thouse.description:='Test';
Thouse.m2:=67
Thouse.obs:='any observation';
blockwrite (file1,Thouse,sizeof(Thouse));
closefile (file1);
...

Why I can't use memo data type?
Any suggestion?
 
hi HMex,

I will try to help you.
First of all, let me suggest writing your type declarations with a leading "T" as this is an often used Delphi convention. And your Memo type is written "TMemo":

type
Thouse=record
id:integer;
description:string[60];
m2:integer;
obs:TMemo;
end;

then declare your instance house of this object THouse:

var
house:Thouse;

when using a record which contains an object (e.g. a TMemo) you have to create this object. So let me propose this code:

whith house do
begin
id:=1;
description:='Test';
m2:=67
obs := TMemo.Create(Application);
obs.Lines.Append('any observation');
end;

when using blockread/blockwrite you cannot read/write directly into/from an object as an object is just a pointer to its content. Instead use its methods to read/write just its content. To use blockwrite anyway try this:

assignfile (file1,'teste.txt');
reset (file1,1);
blockwrite (file1,house,sizeof(Thouse));
for i := 0 to house.obs.Lines.Count-1 do
blockwrite (file1,house.obs.Lines,
Length(house.obs.Lines));
closefile (file1);

But this is not nice as one would never write a StringList (like Memo.Lines) using BlockWrite.
Another way: you can write the content of a memo directly to a text file using the TMemo.Lines.SaveToFile method.
But then you would have to restructure your code....

At the end of this example you have to free the memory of the Memo object inside your house structure:

house.obs.Free;


But there should be better ways when you just use a simple database instead of this.... but this would lead far away now....

good luck
Roderich

 
This record needs to be fixed width. If you were saving your data to a database instead of a fixed-width file, you could use a TStringlist which can grow and shrink as needed.

A TMemo is a visual control. I don't think you want a visual control in your structure.


  house=record
                id:integer;
                description:string[60];
                m2:integer;
                obs:string[200];
        end;


Using a constrained string like that would work here.
TealWren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top