[fpc-pascal]OOP for dummies...
James_Wilson at i2.com
James_Wilson at i2.com
Tue Jun 19 17:35:45 CEST 2001
Thanks for responding. You gave a very lucid and helpful answer.
> Although TCollection is implemented as an object, there is nothing
> very OOP in the way you need to use it. You can think about it as
> an 'array of pointer' with an indeterminate number of elements,
> plus a bunch of subroutines handling it. Unlike with normal arrays,
> you cannot directly reference to its elements but you have methods
> (functions and subroutines) that provide all this functionality and
I realize that a TCollection is not OOP, in the strictest sense, but it
sounded good for a object beginner like me! :-)
> Let's suppose you have
> Coll : TCollection
> CollArray : array [1..MaxCollectionSize] of pointer;
This seems to imply a physical limit (MaxCollectionSize) has been defined.
Is that correct? In rtl\inc\objects.pp I found the following:
MaxBytes = 128*1024*1024;
MaxCollectionSize = MaxBytes DIV SizeOf(Pointer);
if a Pointer is 4 bytes (just guessing) then the upper limit of a
TCollection using MaxCollectionSize would be about 33,554,432?
> Unlike a simple array, you have to initialize the collection:
> new (Coll);
> Coll.Init (Limit, Delta);
This makes sense. But what is the Limit and Delta for?
> To put something into the collection at a given location, use
> Coll.AtPut (Index, ItemPointer)
So this would be the pointer to the "record" itself? For example, suppose
I've defined the following:
MAX_LINES = ???; // if there's no "limit" what should this be?
DataRecord = packed object
Data : string;
Display : boolean;
Actual : longint;
Color : byte;
Index : longint;
DataFromFile : array [0..MAX_LINES] of ^DataRecord;
With an array I can assign values like this:
DataFromFile [Index]^.Data := 'Some data';
DataFromFile [Index]^.Display := TRUE;
DataFromFile [Index]^.Actual := Index;
DataFromFile [Index]^.Color := 23;
But using 'Coll.AtPut (Index, ItemPointer)' how can I assign values to the
different "elements" in the DataRecord structure? Would it be something
BTW; I can easily redo that DataRecord structure if needed. While the 4
pieces of information are essential I'm certainly not adverse to trying
something different their.
> To read from a given location, use
> ItemPointer := Coll.At (Index),
I guess if I can comprehend how to store values I can just reverse the
logic to read them back?
> And, unlike with the array, you have to dispose of the collection
> at the end with
> dispose (Coll);
Yes, this I will do.
> Actually, the collection gives you more than that. There is
> Coll.Insert which keeps track of the items put into the collection
> and places the new item directly at the end; thus, if you want to
> place item after item into the collection, you don't have to keep
> track of the indexes, just call Insert repeatedly. Or there is
> AtInsert which inserts your new item at the position you specify,
> not overwriting what is already there but making room for it,
> shifting every item behind that by one position. Or if you have a
> pointer to an item, you can learn which index it has by calling
This all sounds like what I would like to do, so I guess a TCollection is
indeed what I need.
> For a description of all methods, visit Units.pdf. You'll also find
> examples there.
I've done that a few times now, but unfortunately I was having a bit of a
rough time figuring it all out. I'm going to read it again though, if for
no other reason then to see how many more times it takes before I fully
understand it. :-)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal