boardtc
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.
Thanks, Tom.
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(FileNam e,fmCreate or fmOpenWrite);
try
WriteCollection(AFile,Coll ection);
finally
AFile.Free;
end;
end;
procedure ReadFile(const FileName: string; Collection : TCollection);
var
AFile : TFileStream;
begin
AFile := TFileStream.Create(FileNam e,fmOpenRe ad);
try
ReadCollection(AFile,Colle ction);
finally
AFile.Free;
end;
end;
This works great, thanks. Note it works with or without the (single) RegisterClass(TMyCollectio n).
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.
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(FileNam
try
WriteCollection(AFile,Coll
finally
AFile.Free;
end;
end;
procedure ReadFile(const FileName: string; Collection : TCollection);
var
AFile : TFileStream;
begin
AFile := TFileStream.Create(FileNam
try
ReadCollection(AFile,Colle
finally
AFile.Free;
end;
end;
This works great, thanks. Note it works with or without the (single) RegisterClass(TMyCollectio
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
bleh, too late again :-P
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.
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.
Procedure TForm1.FormCreate(..);
Begin
RegisterClass ( YourCollection );
RegisterClass ( your CollectionItemClass ); // registerClass (TMyCollItem);
End;
To load the Collection
ReadComponentResFile(FileN
To save it ( the Collection should be created before calling this functiuon):
procedure WriteComponentResFile( FileNam,yourCollection);
best Regards
Cesario