Link to home
Start Free TrialLog in
Avatar of boardtc
boardtcFlag for Ireland

asked on

Streaming TOwnedCollection descendant from flat file

I have a TOwnedCollection descendant  that I am using (for my collection items). Can someone point me to some sample code for loading (streaming) my collection from a fla file.

Thanks, Tom.
Avatar of Cesario Lababidi
Cesario Lababidi
Flag of Germany image

Hi tom,


Procedure TForm1.FormCreate(..);
Begin
  RegisterClass ( YourCollection );
 RegisterClass ( your CollectionItemClass );  // registerClass (TMyCollItem);
End;


To load the Collection
ReadComponentResFile(FileName: string,YourCollectio)

To save it ( the Collection should be created before calling this functiuon):
procedure WriteComponentResFile( FileNam,yourCollection);

best Regards

Cesario
Avatar of boardtc

ASKER

Thanks for the note. WriteComponentResFile looks for a TComponent descendant for "yourCollection", I want to pass a TownedCollection descendant. ANy ideas what I need to do? Cheers, tom.
Avatar of Engwi
Engwi

Tom

What does the class declaration look like for your decendant.

Reading the file should be quite easy, just use the data returned with the add function in your class to add items to the collection.

Regards
 Engwi
ASKER CERTIFIED SOLUTION
Avatar of Cesario Lababidi
Cesario Lababidi
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boardtc

ASKER

Engwi,
  TMyCollection = class(TOwnedCollection)
....sure the add gives me the TMyCollectionItem but I am trying to figure out how to stream the date to a file.....

Cesario I added :

procedure WriteFile(const FileName: string; Collection : TCollection);
  var
    AFile : TFileStream;
begin
  AFile := TFileStream.Create(FileName,fmCreate or fmOpenWrite);
  try
    WriteCollection(AFile,Collection);
  finally
    AFile.Free;
  end;
end;

procedure ReadFile(const FileName: string; Collection : TCollection);
  var
    AFile : TFileStream;
begin
  AFile := TFileStream.Create(FileName,fmOpenRead);
  try
    ReadCollection(AFile,Collection);
  finally
    AFile.Free;
  end;
end;

This works great, thanks. Note it works with or without the (single) RegisterClass(TMyCollection).

The resultant file has binary stuff in it and is not very pretty. How can I get it to produce neat output in the file like Delphi does with the dfm?

Thanks a lot,

Tom.


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
bleh, too late again :-P
Avatar of boardtc

ASKER

Sorry you were late Stefaan, I have however converted to yuor method, much cleaner than the TReader . TWriter method, thank you!

The end result is the same. Any way to make the output clean (like delphi with the dfm). I am going to use it as a quick and dirty way of populating a collection so would like a cleaner file than present.

Cheers, Tom.