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
Solved

Need TList Example

Posted on 1998-12-28
10
364 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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