Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to save a TCollectionItem from a component to a stream Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Files Operation
Language
Delphi 2.x
Views
70
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have written a component which uses TCollections as properties. Now I need a way 
to save the TCollectionItems to a file. How can I do that?

Answer:

You may try these routines. I have tested them with TStatusBar.Panels collection 
and they worked for me:
1   
2   procedure LoadCollectionFromStream(Stream: TStream; Collection: TCollection);
3   begin
4     with TReader.Create(Stream, 4096) do
5     try
6       CheckValue(vaCollection);
7       ReadCollection(Collection);
8     finally
9       Free;
10    end;
11  end;
12  
13  procedure LoadCollectionFromFile(const FileName: string; Collection: TCollection);
14  var
15    FS: TFileStream;
16  begin
17    FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
18    try
19      LoadCollectionFromStream(FS, Collection);
20    finally
21      FS.Free;
22    end;
23  end;
24  
25  procedure SaveCollectionToStream(Collection: TCollection; Stream: TStream);
26  begin
27    with TWriter.Create(Stream, 4096) do
28    try
29      WriteCollection(Collection);
30    finally
31      Free;
32    end;
33  end;
34  
35  procedure SaveCollectionToFile(Collection: TCollection; const FileName: string);
36  var
37    FS: TFileStream;
38  begin
39    FS := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
40    try
41      SaveCollectionToStream(Collection, FS);
42    finally
43      FS.Free;
44    end;
45  end;


Note: It's obvious, the Collection variable must point to the initialized instance 
of a TCollection
descendant.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC