Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating own class from Tlist

Posted on 1999-07-14
1
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

721 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