Solved

Saving a TObjectList to File

Posted on 2007-11-30
4
1,440 Views
Last Modified: 2013-11-23
Hi,
In hte attached code - is there a way to save "MyList" to a file. Of course I would want to retrieve MyList from the file as well.

Any help is appreciated

Thanks
Romans
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Contnrs;
 
type
TOccurrence = class
    Word     : String;
    Count    : Integer;
    Category : Integer;
end;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    MyList        : TObjectList;
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
Var
    MyOcc: TOccurrence;
begin
    MyOcc           := TOccurrence.Create;
    MyOcc.Word      := 'One';
    MyOcc.Count     := 12;
    MyOcc.Category  := 1;
    MyList.Add(MyOcc);
 
    MyOcc           := TOccurrence.Create;
    MyOcc.Word      := 'Two';
    MyOcc.Count     := 5;
    MyOcc.Category  := 2;
    MyList.Add(MyOcc);
 
    // Save MyList to a file here .... ??????
 
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
    MyList := TObjectList.Create;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
    MyList.Free;
end;
 
end.

Open in new window

0
Comment
Question by:Romans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20384342
you could save this to disk, but you'll have to make it yourself.
Before you try that, some another question:
why is TOccurence a class and not a record?
0
 
LVL 3

Expert Comment

by:blckholehorizon
ID: 20476096
inherit from TComponent and use the Read Component and Write Component built in functions.  You must register this class via the 'registerclass' function so that the RTTI is published with the compiler.  All variables to be saved to disk must be in the published section of the class.

Instead of creating multiple instances of the same class, why dont you inherit from TCollection and TCollectionItem.

If this question is still alive, I will publish an example of streaming TCollection and TCollectionItem to and from disk.
0
 

Author Comment

by:Romans
ID: 20484023
Yes - the question is still alive!

Currently - I have written this to a text file using TStringlist. Although this works - it exposes the content of the file to the users via textpad etc. I am hoping that by writing "MyList" directly to a file will help eliminate this problem.

In addition - I expect the text file to grow large both in size and the number of lines. The file I am currently using for development is currently 14,000 lines (400 Kb) and still growing. Accessing the file contents quickly is important too.

Have I assigned enough points for this question?

Romans
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 125 total points
ID: 20484115
ok, first of all, writing objects to disk will not render the final file unreadable, if you want that, you'll have to encrypt it yourself. I'd suggest you look here for that: http://cityinthesky.co.uk/cryptography.html

If you want to access the contents quickly you'll have to decide where you want to go. You could simply read the whole file into memory (the fastest). There are also other solutions, but they depend on how you write your file to disk, so I'd suggest you first look into that.

Do you think the contents of TOccurence will change alot, if it would change, how much value do you put into backwards compatibility (ability to read 'old format' files)?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question