[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1474
  • Last Modified:

Saving a TObjectList to File

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
Romans
Asked:
Romans
  • 2
1 Solution
 
MerijnBSr. Software EngineerCommented:
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
 
blckholehorizonCommented:
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
 
RomansAuthor Commented:
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
 
MerijnBSr. Software EngineerCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now