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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
philipleighsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.