Board index » delphi » HELP: TList,TCollection and pointers

HELP: TList,TCollection and pointers

Hello everybody!
I have a problem with the TList object in Delphi and I would be very
thankful if you could help me resolving the problem and the online
tutorials where I could read more explanations about the problem. I have
done several researches with Google but the tutorials I found didn't
document well the TList and TCollection classes. I only found some texts
with instructions not applicable to my problem.

I have a list of objects (defined by myself). At the moment of exiting
the program, or with the explicit "save" command, the list must be saved
in a text file. The next time I start the program the data from the file
must be loaded back in the list of objects. But I have problems with the
pointer casting to get the each object from the list. The similar
problem I have also with the TCollection class.
Can anybody help me? Thanks in advance.

Stiv

-----------------------------------------------------------

Here is a reduced version of the procedure I use to save the data from
the list to the file. I've also put here (commented) the metods I had
try to resolve the problem.

   procedure managerCampi.salva;

// The list  "lstOggetti"  is global in this program.
     var fileBackup: TextFile;
     strRiga: String;
     i: integer;
     objAttuale: attributi; {The object from which I want to get the
"nome" property; it should get the values of every element of the list.
The definition of the "attributi" class is in another file (I used the
instruction "use")}
     p_objAttuale: ^attributi;

     begin
       AssignFile(fileBackup, 'foo.bak');
       Rewrite(fileBackup);

       for i:=0 to (lstOggetti.Count - 1) do
         begin

{        objAttuale:=attributi(lstOggetti[i]);  Doesn't work!}

{                 p_objAttuale := (lstOggetti.Items[i]); Doesn't work!
         objAttuale := p_objAttuale^;}

{        objAttuale:=(lstOggetti.Items[i] as attributi);  Doesn't work}

              strRiga:= objAttuale.nome;
              writeln(fileBackup, strRiga );
         end;

       CloseFile(fileBackup);

     end;

 

Re:HELP: TList,TCollection and pointers


Quote
"Stiv" <ab...@127.0.0.1> wrote in message

news:81Hla.12737$DN.347339@tornado.fastwebnet.it...

Quote
> Hello everybody!
> I have a problem with the TList object in Delphi and I would be very
>      objAttuale: attributi; {The object from which I want to get the
> "nome" property; it should get the values of every element of the list.
> The definition of the "attributi" class is in another file (I used the
> instruction "use")}

Shouldn't it be 'uses'?

Quote
> {        objAttuale:=attributi(lstOggetti[i]);  Doesn't work!}

> {                 p_objAttuale := (lstOggetti.Items[i]); Doesn't work!

> {        objAttuale:=(lstOggetti.Items[i] as attributi);  Doesn't work}

Andres

Re:HELP: TList,TCollection and pointers


Quote
"Stiv" <ab...@127.0.0.1> wrote in message

news:81Hla.12737$DN.347339@tornado.fastwebnet.it...

Quote
> Hello everybody!
> I have a problem with the TList object in Delphi and I would be very
> thankful if you could help me resolving the problem and the online
> tutorials where I could read more explanations about the problem. I have
> done several researches with Google but the tutorials I found didn't
> document well the TList and TCollection classes. I only found some texts
> with instructions not applicable to my problem.

> I have a list of objects (defined by myself). At the moment of exiting
> the program, or with the explicit "save" command, the list must be saved
> in a text file. The next time I start the program the data from the file
> must be loaded back in the list of objects. But I have problems with the
> pointer casting to get the each object from the list. The similar
> problem I have also with the TCollection class.
> Can anybody help me? Thanks in advance.

> Stiv

> -----------------------------------------------------------

> Here is a reduced version of the procedure I use to save the data from
> the list to the file. I've also put here (commented) the metods I had
> try to resolve the problem.

>    procedure managerCampi.salva;

> // The list  "lstOggetti"  is global in this program.
>      var fileBackup: TextFile;
>      strRiga: String;
>      i: integer;
>      objAttuale: attributi; {The object from which I want to get the
> "nome" property; it should get the values of every element of the list.
> The definition of the "attributi" class is in another file (I used the
> instruction "use")}
>      p_objAttuale: ^attributi;

>      begin
>        AssignFile(fileBackup, 'foo.bak');
>        Rewrite(fileBackup);

>        for i:=0 to (lstOggetti.Count - 1) do
>          begin

> {        objAttuale:=attributi(lstOggetti[i]);  Doesn't work!}

How do you mean, it doesn't work? What error do you get? How is exactly
implementet the class "attrbuti"?

- Show quoted text -

Quote

> {                 p_objAttuale := (lstOggetti.Items[i]); Doesn't work!
>          objAttuale := p_objAttuale^;}

> {        objAttuale:=(lstOggetti.Items[i] as attributi);  Doesn't work}

>               strRiga:= objAttuale.nome;
>               writeln(fileBackup, strRiga );
>          end;

>        CloseFile(fileBackup);

>      end;

Re:HELP: TList,TCollection and pointers


Quote
Andres Aitsen wrote:
>>The definition of the "attributi" class is in another file (I used the
>>instruction "use")}

> Shouldn't it be 'uses'?

It was a type error. However, I did it. The program works now. Thank you.

Re:HELP: TList,TCollection and pointers


Quote
Ante Smolcic wrote:
> How do you mean, it doesn't work? What error do you get? How is exactly
> implementet the class "attrbuti"?

I did it. The program works now. Thanks to all the people on the net who
helped me.
Hvala, Ante. ;-)

Other Threads