You're not actually saving "objects" to files/databases but the contents
of those objects, and in most cases the contents is going to be string,
integer, Boolean. If you need to google for this, try googling for
"object serialization".
There are a number of methods that may be used with Delphi objects, and,
in fact, Delphi provides a method "out of the box": TStream
serialization using TPersistent descendents. I never use this method - I
prefer a different method:
Here's how I usually set things up so I can write/read objects to an
generic TStream. I use a pair of methods for this, WriteToStream and
ReadFromStream. All objects that need to be "streamed" will implement
those two methods. Objects containing objects will simply call the
"WriteToStream" method on the contained object. You can save anything to
a stream using this method, and it doesn't need to be TPersistent. Also
it will use less space in the actual stream.
<code>
TAssignment = class
Public
AssignmentName:string;
Procedure SaveToStream(W:TWriter);
Procedure ReadFromStream(R:TReader);
End;
TAssignmentList = class(TObjectList)
Public
Procedure SaveToStream(W:TWriter);
Procedure ReadFromStream(R:TReader);
End;
TStudent = class
Public
Procedure SaveToStream(W:TWriter);
Procedure ReadFromStream(R:TReader);
End;
TStudentList = class(TObjectList)
Public
Procedure SaveToStream(W:TWriter);
Procedure ReadFromStream(R:TReader);
End;
Procedure TAssignement.SaveToStream(W:TWriter);
Begin
W.WriteInteger(0); // structure version - allows for easy upgrading of
already streamed structures
W.WriteString(AssignmentName);
End;
Procedure TAssignement.ReadFromStream(R:TReader);
Var ver:Integer;
Begin
Ver := R.ReadInteger;
If Ver > 0 then raise Exception.Create('Stream saved with a newer
version of this program');
AssignmentName := R.ReadString;
End;
Procedure TAssignementList.SaveToStream(W:TWriter);
Var i:Integer;
Begin
W.WriteInteger(0); // versioning
W.WriteInteger(Count); // Number of objects I'll be saving
For i:=0 to Count-1 do
TAssignement(Items[i]).SaveToStream(W);
End;
Procedure TAssignementList.ReadFromStream(R:TReader);
Var Ver,I,Cnt:Integer;
A:TAssignement;
Begin
Ver := R.ReadInteger;
If Ver > 0 then raise Exception.Create('Stream saved with a newer
version of this program');
Cnt := R.ReadInteger; // How many objects have been previously saved?
For i:=1 to Cnt do
Begin
A := TAssignement.Create;
A.ReadFromStream(R);
Add(A);
End;
End;
</code>
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Alan Colburn
Sent: Saturday, May 19, 2007 11:27 PM
To: [email protected]
Subject: Re: OOP -- Persisting Lists
Suppose your application needs a list of lists. I'm having trouble
figuring out how and where one persists object data--whether using a
database or some other file format for storage.
Many examples illustrate the point but, for the sake of argument,
imagine a classroom with students completing assignments. An Assignment
class could include fields for pointsEarned, pointsPossible, etc. A
Student class would include a field containing a (TObjectList descended)
list of Assignment objects. A Classroom object, in turn, would include a
field containing (TObjectList descended) list of Student objects.
Now, how does one go about saving all this in a file? It seems like a
Classroom object, when saved, would need a field made up of Student
objects which, in turn, requires a field of Assignment objects.
Can you sense my confusion? I'm used to database (or file) fields made
up of strings, integers, memos, etc. I don't understand how fields made
up of object lists would work ...
As always, thanks in advance -- Al
_________________________________________________________________
Add some color. Personalize your inbox with your favorite colors.
www.windowslive-hotmail.com/learnmore/personalize.html?locale=en-us&ocid
=TXT_TAGLM_HMWL_reten_addcolor_0507
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi