Using collections. Part1.


© Lyapin Ilya

This article starts new series that is dedicated to some useful VCL classes.

Now let' look at TCollection class. TCollection is a container for TCollectionItem objects. The VCL controls (such as TDBGrid, TListView, TStatusBar, etc) widely use objects derived from TCollection.

TCollection features.

1. Easy to use container for structured data.

Each TCollection holds a group of TCollectionItem objects. You can access TCollectionItem objects by zero-based index (Items property). Items can be added into a collection using Add and Insert methods. Clear method deletes all items from the collection.

Objects descended from TCollection can contain objects descended from TCollectionItem. For example, a TCheckConstraints object contains TCheckConstraint objects; these two classes are used by TDataSet to represent record-level constraints to the dataset.

2. Built-in storing properties of TCollection class.

TCollection itself is TPersistent descendant so a property of TCollection derived class can be saved in DFM file and loaded from it. The controls that use TCollection and TCollectionItem descendants have a published property that holds a collection. (For example, the Columns property of TListView holds a TListColumns.)

3. Built-in design-mode support.

The VCL register a standard property editor (Collection editor) that can be invoked from the Object Inspector to edit the items in the collection. The editor works with all TCollection descendants.
TCollection is a great help when writing new component.

Code example.

Type
// Forward declarations
TMyCollectionItem=class;
TMyCollection=class;


TMyCollection=class(TCollection)
private
function GetItem(Index: Integer): TMyCollectionItem;
procedure SetItem(Index: Integer; const Value: TMyCollectionItem);
public
Constructor Create;
property Items[Index: Integer]: TMyCollectionItem read GetItem write SetItem; default;
end;

TMyCollectionItem=class(TCollectionItem)
private
FString:String;
FBool: Boolean;
FInt:integer;
FObj:TSomeClass;
Procedure SetObj(const Value: TSomeClass);
public
procedure Assign(Source:TPersistent);override;
constructor Create(AOwner:TCollection);override;
destructor destroy;override;
published
property BoolProp:Boolean read FBool write FBool;
property StringProp:String read FString write FString;
property IntProp:integer read FInt write FInt;
property ObjProp: TSomeClass read FObj write SetObj;
end;

{ TMyCollection }

constructor TMyCollection.Create;
begin
inherited Create(TMyCollectionItem);
end;

function TMyCollection.GetItem(Index: Integer): TMyCollectionItem;
begin
Result:=TMyCollectionItem(inherited GetItem(Index));
end;

procedure TMyCollection.SetItem(Index: Integer; const Value: TMyCollectionItem);
begin
inherited SetItem(index,value);
end;

{ TMyCollectionItem }

procedure TMyCollectionItem.Assign(Source: TPersistent);
begin
if Source is TMyCollectionItem then
with Source as TMyCollectionItem do
begin
FString:=StringProp;
FBool:=BoolProp;
FInt:;=Intprop
FObj.Assign(ObjProp);
end
else
inherited Assign(source);
end;

constructor TMyCollectionItem.Create(AOwner: TCollection);
begin
inherited Create(AOwner);
FObj:=TSomeClass.Create(self);
end;

destructor TMyCollectionItem.destroy;
begin
inherited;
FObj.Free;
end;

procedure TMyCollectionItem. SetObj(const Value: TSomeClass);
begin
FEntries.Assign(Value);
end;

Code explanation.

It's clear that at least two classes should be created:
1. Collection class (TMyCollection) derived from TCollection.
TMyCollection overrides Items property just to avoid type conversation each time when accessing collection item.
Constructor TMyCollection.Create defines class of TCollectionItem' stored.
2. Collection item (TMyCollectionItem) class derived from TCollectionItem
TMyCollectionItem overrides virtual (!) constructor of TCollectionItem. It's important because collection class should create TMyCollectionItem instances instead of TCollectionItem.
Another important method that should be overridden - Assign. Assign method should implement correct assignment of properties - see ObjProp.
This example is somewhat unnatural, see next article for more collections!

The program is written for Borland Delphi 4, but this code must work under Delphi 2/3/4/5.
I would like to answer any questions that you may have.

Go To Page: 1


The copyright of the article Using collections. Part1. in Delphi Programming is owned by . Permission to republish Using collections. Part1. in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo