Solved

Need TList Example

Posted on 1998-12-28
10
363 Views
Last Modified: 2011-09-20
Hi,

I'm trying to create an object derived from TList that will act as a container for other objects.
For example the object TEmails would be derived from TList. The objects in TEmails would be TMessage objects, derived from TObject.

How do I create TMessage objects by using Add from TEmails (TList) at runtime and how could I access their properties and methods?

Can you help me with an example?

Thanks.
0
Comment
Question by:michaelsanders
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 200 total points
ID: 1353451
Here is this source for TObjectList I wrote recently. Either rename TObject to TEmail, or derive from it and override the get/put/last methods and items property I have done.

Then just add objects to it like this

TEmailList.Add(TEmail.Create);
TEmail.Last.MyProp := 'Fred';
or

MyEmail := TEmail.Create;
TEmailList.Add(MyEmail );
MyEmail.MyProp := 'Fred';

If you have any more Q's just yell!

Cheers,

Raymond.



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;

implementation


{ 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;
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353452
Oops,

Better I menat TMessage when I said TEmail and EmailList when I said TEmailList. EmailList would of course be an instance of TEmailList... (which would be a TObject list modified in the ways I mentioned in my answer)

You can treat it just as a normal TList:

MyEmailList[i].MyProp := 'Fred'; // etc

Cheers,

Raymond.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353453
Hi Raymond, you're too fast...
Are you just sitting at home and do nothing but wait for new questions?   :-)))

Regards, Madshi.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353454
No Madshi, just the luck of the draw!

Anyway, after cleaning off Dwwang's tire marks I thought it was time I chalked up a few more ;-)

Cheers,

Raymond.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353455
:-)

If I look at the currently locked questions, you'll have success with that...

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:Oli2
ID: 1353456
That's something I've been wondering for a long time...
how do you guys find so much time to answer all these questions?
I mean, you've got to be online 24 h/day and reloading http://.../.../comp/lang/delphi/ 
every 30 sec. ?
;-))

Greetings, Oli
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1353457
Hi, I heard someone talking about me, right? :-)

Anyway, I will leave this site soon, that's why I utilize these ten days to get higher and higher on the ranking list - to let my freinds see me for longer time.

Raymond, I think you could easily get in front of me within few days in the year 1999. By the way, my English is yet not so good, so would you tell me what means "cleaning off Dwwang's tire marks"? :-)

Cheers,
Season's greeting from China!

Wang
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1353458
Hi Wang,

what do you mean with you will leave this site soon? Do you mean you don't like us anymore or what?

Regards, Madshi.

P.S: I had to start my translator program to understand what Raymond meant...   :-)))
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1353459
No, I like all of you very much, also this site very much. However, I need to go now, to leave Internet, computer, and all of you. Tommorow you will see my message saying farewell to all.

Regards, Wang

P.S. I think raymond could even get in front of me within the year 1998. :-)))
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353460
We will be sad to see you leave Wang - you contribute a high level of expertise to the answers provided here.

Cheers,

Raymond.

PS: 'tire marks' is a way of saying you caught up with me and went past so fast you ran over me! :-) :-)

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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