[fpc-pascal]OOP for dummies...

Gabor DEAK JAHN djg at tramontana.co.hu
Sat Jun 16 04:52:16 CEST 2001

At 6/15/01 08:03 AM, you wrote:


 > I need to read the file into memory and then access the data via some type
 > of index. In an array I can use ArrayName [Index] to point to any 
element of
 > the array but I can't seem to duplicate that functionality for a 'dynamic'
 > structure.

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 more.

Let's suppose you have

   Coll : TCollection
   CollArray : array [1..MaxCollectionSize] of pointer;

Unlike a simple array, you have to initialize the collection:

   new (Coll);
   Coll.Init (Limit, Delta);

To put something into the collection at a given location, use

   Coll.AtPut (Index, ItemPointer),

this would be equivalent to

   CollArray [Index] := ItemPointer

To read from a given location, use

   ItemPointer := Coll.At (Index),

which would be

   ItemPointer := CollArray [Index]

And, unlike with the array, you have to dispose of the collection at the end

   dispose (Coll);

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 Coll.IndexOf.

For a description of all methods, visit Units.pdf. You'll also find examples


   Gabor DEAK JAHN

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

More information about the fpc-pascal mailing list