Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

overriding or replacing the Items property in TObjectList

609 views
Skip to first unread message

Roger McNaught

unread,
Jul 18, 2003, 6:03:26 PM7/18/03
to
Hi all,

I am writing a component that is derived from TObjectList (although that
might change to another type of container), and I want to override the Items
property so that I don't have to typecast everywhere.

What i have is something like this:
TXYZList = class (TObjectList)
..
end;

TXYZ = class (TObject)
public
property SomeProp
end;


So I am writing everywhere
(xyz[i] as TXYZ).SomeProp := blah;

but what I would like to write is
xyz[i].someProp := blah;

(You can probably guess that it gets very tedious, and the lines get very
long, and it happens alot!)

In short, I'd like to override the Items property, but I can't do that.

I feel sure there must be a fairly simple way of overriding/replacing the
Items and avoiding the typecasting, but I don't know what it is.

I'd appreciate a helpful suggestion or two

Regards

Roger


Rob Kennedy

unread,
Jul 18, 2003, 7:53:35 PM7/18/03
to
Roger McNaught wrote:
> In short, I'd like to override the Items property, but I can't do that.

Yes you can. In fact, TObjectList already overrides that property.
TList.Items is of type Pointer, but TObjectList.Items is of type
TObject. The overriding isn't polymorphic, though, so if you're
accessing the list via a generic TObjectList variable, then you'll be
using the TObject-typed Items property. To get a TXYZ-typed Items
property, you'll need to use a variable declared with the TXYZList type
explicitly.

(Actually, "override" probably isn't the best word in this context. If
we were dealing with methods instead of properties, the compiler would
issue a warning that we're hiding something from the base class. When
you "override" the Items property, then, you're really "reintroducing" it.)

--
Rob

Alexander Bauer

unread,
Jul 18, 2003, 8:05:14 PM7/18/03
to
Hi Roger,

You just need to do the casting in the TXYZListClass.
Therefore you need to write a new ListClass like this: (I used TList for
example):

TXYZ = class (TObject)
public
property SomeProp
end;

TXYZListClass = Class(TList)
protected
function Get(Index: integer): TXYZ; virtual; // made this virtual to be
able to override in possible descendants because in TList these functions
are dynamic or static - don't remember exactly.
procedure Put(Index: integer; Item: TXYZ); virtual;
public
procedure Clear; override;
property Items[Index: integer]: TXYZread Get write Put; default;
end;


procedure TXYZListClass.Clear;
var i: integer;
begin
for i := 0 to Count-1 do TXYZ(Items[i]).Free; // Since TXYZ is a class not
only a pointer to what ever, we need to free the item instances before
clearing the list.
inherited Clear;
end;

function TXYZListClass.Get(Index: integer): TXYZ;
begin
Result := TXYZ(inherited Get(Index));
end;

procedure TXYZListClass.Put(Index: integer; Item: TXYZ);
begin
inherited Put (Index,Pointer(Item));
end;


Then override the property Items of you container class:


TYourContainerClass = Class(??)
private
fItems: TXYZListClass;
...
public
property Items: TXYZListClass read;
...
end;

Constructor TYourContainerClass.Create(aOwner: TComponent);
begin
inherited Create (aOwner);
fItems := TXYZListClass.Create;
...
end;

destructor TYourContainerClass.Destroy;
begin
if fItems <> nil then fItems.Free;
...
inherited Destroy;
end;


I hope this can help you.

Regards,
Alexander Bauer

"Roger McNaught" <not_roger_mcnaught@not_yahoo.com> schrieb im Newsbeitrag
news:3f186eae$1...@newsgroups.borland.com...

Roger McNaught

unread,
Jul 23, 2003, 2:33:47 AM7/23/03
to
Hi Ron and Alexander

Thanks for your replies.

Unfortunately a family crisis over the weekend has kept me from pursuing the
matter any further since then.

But I'm now about to start working on the matter again, and will be going
through your advice.

Again, many thanks

Roger

"Roger McNaught" <not_roger_mcnaught@not_yahoo.com> wrote in message
news:3f186eae$1...@newsgroups.borland.com...

0 new messages