Solved

Saving a TObjectList to File

Posted on 2007-11-30
4
1,383 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now