Solved

Saving a TObjectList to File

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Debugging a PHP Web Application 2 106
Android Studio - Gradle project sync failed 5 3,254
java class in read only mode in eclipse 2 155
Best IDE for PHP starters 11 123
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. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes 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.

773 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