Wanting to create a TList subclass...

Hi! I'm rather new to classes and OOP, and I would like to create a class (TScriptObjectList) that is identical to TList BESIDES the fact that I want the Items property to return a pointer to the TScriptObject class instead (a class that I've created).
Can I accomplish that without having to duplicate the entire TList class? Overriding perhaps?

Like:
var
  MyScriptObject : TScriptObject;

MyScriptObject := MyScript.Children.Items[0];
(no typecasting needed!)

Thanks!

Daniel, Sweden
DippenAsked:
Who is Participating?
 
philipleighsConnect With a Mentor Commented:
You can do it like this. I have a class called TDialogItem which would be TScriptObject for you.

You can now use all TList methods and properties with TScriptObject variables instead of Pointers.

Cheers,
Phil.


interface

....

type
  TDialogItemList = class(TList)
    protected
      function Get(AIndex: Integer): TDialogItem;
      procedure Put(AIndex: Integer; ADialogItem: TDialogItem);
    public
      property Items[Index: Integer]: TDialogItem read Get write Put;
  end;



....

implementation

....

function TDialogItemList.Get(AIndex: Integer): TDialogItem;
  begin
    Result := inherited Get(AIndex);
  end;

procedure TDialogItemList.Put(AIndex: Integer; ADialogItem: TDialogItem);
  begin
    inherited Put(AIndex, ADialogItem);
  end;

end.
0
 
snoop_doggy_doggCommented:
Hey dude, I've done this before...you can't override the getter for TList, but you can redeclare the property getter and setter in your subclass to only allow TScriptObjects as long as whoever has your list object knows it as a TScriptList instead of a TList.  You also might want to look at TCollection and TCollectionItem...it might be more appropriate for what you're doing.

Cheers,
Woof!
0
All Courses

From novice to tech pro — start learning today.