[fpc-pascal]OOP for dummies...

Gabor DEAK JAHN djg at tramontana.co.hu
Wed Jun 20 18:11:36 CEST 2001

At 6/19/01 11:35 AM, you wrote:


 > I realize that a TCollection is not OOP, in the strictest sense, but it
 > sounded good for a object beginner like me!  :-)

It is OOP, it is implemented as an object after all, just that it does not
present many OOP challenges.

 > if a Pointer is 4 bytes (just guessing) then the upper limit of a
 > TCollection using MaxCollectionSize would be about 33,554,432?

I guess so, I never calculated but you can always learn the actual value
with a simple writeln (MaxCollectionSize).

 > This makes sense. But what is the Limit and Delta for?

Limit is the initial size and Delta is the increment it will be enlarged by
if you go beyond Limit. They serve reasons of efficiency. If you know that
you'll have around 5,000 items why waste memory for much more? Similarly, if
you do go above the initial Limit, it is much more efficient to ask for a
larger additional chunk (in this example, for instance, Delta could be
1,000), then Limit will practically become 6,000 and you have 1,000 more
items to go before the Collection needs housekeeping again.

 > Coll.AtPut (Index,ItemPointer.Data);

Not exactly, you add all the information you want to your record first, then
put only its address into the collection,

Think about it this way: let's suppose Pascal is modified and does not allow
constructing an array of records, only of simple types like integers or
pointers. What you could do then would be to create an array of pointers, not
the record themselves, which point to somewhere in memory where the actual
contents of the record is stored.

   PDataRecord = ^TDataRecord
   TDataRecord = record ...
   TArray = array [1..MaxSize] of PDataRecord;
   DataRecord : PDataRecord;
   DataArray : TDataArray;

You would do the same then, for each element of the array, you have to ask
for a record in memory:

   new (DataRecord);

fill it with the required contents:

   DataRecord^.Data := 'Some data';
   DataRecord^.Display := TRUE;
   DataRecord^.Actual  := Index;
   DataRecord^.Color := 23;

then place its address into the array:

   DataArray [Index] := DataRecord;

With the Collection, it is the same, only the last line is different: you
simply use a different syntax to put something into a collection than into
an array. But its meaning is more or less the same:

   Coll.AtPut (Index, DataRecord);

At then end, you have to dispose of your records as well, not only the

 > I guess if I can comprehend how to store values I can just reverse the
logic to read them back?

You get the pointer back, then use it to access your data:

DataRecord := Coll.At (Index),
writeln (DataRecord^.Data);
if DataRecord^.Display then ...


Gabor DEAK JAHN -- Budapest, Hungary.
E-mail: djg at tramontana.co.hu

More information about the fpc-pascal mailing list