?
Solved

Creating own class from Tlist

Posted on 1999-07-14
1
Medium Priority
?
234 Views
Last Modified: 2010-04-06
I have class:

type
  TMyList = class(TList)
  .
  .
  .
end;

Is there something I have to know/do before I can use my class? When I see components, there are always something like:
  .
  .
  public
    constructor Create(AOwner: TComponent); override;
  .
  .
constructor TSampleCalendar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

What are these inherited things? Do I have to use these things when i make my own class or is it just with components? How about if I have TList inside myclass:
type
  TTemp = class
   private
     mylist: TList;
end;

I usually just create the list when I add items into it. Like this

if mylist = nil then
  mylist := TList.Create;
mylist.add(item);
0
Comment
Question by:tonitop
1 Comment
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 200 total points
ID: 1388632
Making your own TList descendents is useful as it allows you to deal with the particular type more easily.

Inherited means called the inherited method. Its useful eg: in virtual functions where you modify some behaviour then call the method in the base class.

Here is an example: (TObhjectList is a TList descendant that stores TObjects. It is trivial to derive from this to make a list that stores TMYObjects!)

type
  TObjectList = class(TList)
    private
      FOwnObjects : Boolean;

    protected
      function Get(Index : Integer) : TObject;
      procedure Put(Index : Integer;
                    AObject : TObject);
    public
      property Items[Index : integer] : TObject read get write put; default;
      property OwnObjects : Boolean read FOwnObjects write FOwnObjects;

      constructor Create;
      destructor Destroy; Override;

      Function First : TObject;
      Function Last : TObject;
      Procedure Clear;
  end;


{ TObjectList }

Constructor TObjectList.Create;

begin
  Inherited;

  FOwnObjects := True;
end;

destructor TObjectList.Destroy;
var
  I : Integer;
begin
  if FOwnObjects then // better clobber them
    for I := 0 to Count - 1 do
      Items[i].Free;

  Inherited;
end;

function TObjectList.Get(Index : Integer) : TObject;

begin
  Result := TObject(Inherited Get(Index));
end;

procedure TObjectList.Put(Index : Integer;
                                    AObject : TObject);
begin
  Inherited Put(Index, AObject);
end;

Function TObjectList.Last : TObject;

begin
  Result := TObject(Inherited Last);
end;

Function TObjectList.First : TObject;

begin
  Result := TObject(Inherited First);
end;

Procedure TObjectList.Clear;
var
  I : Integer;
begin
  if FOwnObjects then // better clobber them
    for I := 0 to Count - 1 do
      Items[i].Free;

  Inherited;
end;

Cheers,

Raymond.

0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question