Solved

Saving a TObjectList to File

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is an NDK ? 3 94
Remove unused usings from code in MonoDevelop 8 436
notepad++ shortcuts 6 135
IronPython and Visual Studio 2013+ 2 92
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

863 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

27 Experts available now in Live!

Get 1:1 Help Now