Board index » delphi » String list error

String list error

Hi all,

I'm having big problems with getting records to actually save.  I've defined
a new record type like this:

type
TRec = record
Title: String[255];
StrLst: TStringList;
end;

Then I try to write a record (ARec) to the file (TheFile) and it works.  I
load it back, displaying the value Title in a textbox, Edit1.  This all
works wonderfully.  Here is the code:

var
ARec: TRec;
TheFile: file of TRec;

begin
AssignFile(TheFile,'data.dat');
Reset(TheFile);
while not Eof(TheFile) do begin
    Read(TheFile, ARec);
    Edit1.Text := ARec.Title;  { this works }
end;
CloseFile(EmailOpen);
end;

I then try to access the StringList and get violations, illegal operations
and such like all over the place.  All I'm doing is adding this line:

RichEdit1.Lines.Add(AEmail.StrLst.Text);

and many other variations of it (Values[0], Strings[0], etc) underneath the
Edit1.Text :=.... line in the code above.  It doesn't seem to like accessing
the StringList, even though the normal String works fine.  Can anybody tell
me why and possible how to fix this problem?

Any help greatly appreciated,

--
Tags: af #1730 (remove SPAM to reply)
EAC Paradox Division
html help - http://www.limitwebdesign.co.uk/htmlhelp/
--
"i've been too honest with myself
i should have lied like everybody else"

 

Re:String list error


Quote
Andrew Cameron wrote in message <9if24t$i4q1...@ID-83402.news.dfncis.de>...
>Hi all,

>I'm having big problems with getting records to actually save.

I'm an idiot.  I'm not having problems getting records to save, as the rest
of the post confirms.  That'll teach me not to *think* when posting.  The
remainder of the post still stands.  Yes, I'm an idiot  :)

--
Tags: af #1730 (remove SPAM to reply)
EAC Paradox Division
html help - http://www.limitwebdesign.co.uk/htmlhelp/
--
"i've been too honest with myself
i should have lied like everybody else"

Re:String list error


Quote
"Andrew Cameron" <tagsS...@limitwebdesign.co.uk> wrote in message

> type
> TRec = record
> Title: String[255];
> StrLst: TStringList;
> end;

tStringList and other objects are implemented as pointers so you can't
simply read and write records that contain them. What you need to implement
is something like

procedure WriteTRec (var aFile : TextFile; aRec : tRec);

var    i    : integer;

begin
Writeln (aFile, aRec.Title);
Writeln (aFile, aRec.StrLst.Count);
for i := 0 to (aRec.StrList.Count - 1) do
    Writeln (aFile, aRec.StrLst [i]);
end;

procedure ReadTRec (var aFile : TextFile; var aRec : tRec);

var    cnt, i : integer;
        aStr : string;

begin
Readln (aFile, aRec.Title);
Readln (aFile, cnt);
aRec.StrLst.Clear;
for i := 1 to cnt do
    begin
    Readln (aFile, aStr);
    aRec.StrLst.Add (aStr);
    end;
end;

Re:String list error


Maybe this helps:

ARec.StrLst :=  TStringList.Create;
try
  ARec.StrLst.Add('First line');
  RichEdit1.Lines.Add(ARec.StrLst.Text);
finally
  ARec.StrLst.Free;
end;

Regards,

Erwin Denissen <erw...@high-logic.com>

High-Logic, The Netherlands
http://www.high-logic.com/

If you want to stay informed about the Font Creator Program -- subscribe
yourself to this list:
http://hlfcp.listbot.com/

Andrew Cameron <tagsS...@limitwebdesign.co.uk> schreef in berichtnieuws
9if24t$i4q1...@ID-83402.news.dfncis.de...

Quote
> Hi all,

> I'm having big problems with getting records to actually save.  I've
defined
> a new record type like this:

> type
> TRec = record
> Title: String[255];
> StrLst: TStringList;
> end;

> Then I try to write a record (ARec) to the file (TheFile) and it works.  I
> load it back, displaying the value Title in a textbox, Edit1.  This all
> works wonderfully.  Here is the code:

> var
> ARec: TRec;
> TheFile: file of TRec;

> begin
> AssignFile(TheFile,'data.dat');
> Reset(TheFile);
> while not Eof(TheFile) do begin
>     Read(TheFile, ARec);
>     Edit1.Text := ARec.Title;  { this works }
> end;
> CloseFile(EmailOpen);
> end;

> I then try to access the StringList and get violations, illegal operations
> and such like all over the place.  All I'm doing is adding this line:

> RichEdit1.Lines.Add(AEmail.StrLst.Text);

> and many other variations of it (Values[0], Strings[0], etc) underneath
the
> Edit1.Text :=.... line in the code above.  It doesn't seem to like
accessing
> the StringList, even though the normal String works fine.  Can anybody
tell
> me why and possible how to fix this problem?

> Any help greatly appreciated,

> --
> Tags: af #1730 (remove SPAM to reply)
> EAC Paradox Division
> html help - http://www.limitwebdesign.co.uk/htmlhelp/
> --
> "i've been too honest with myself
> i should have lied like everybody else"

Other Threads