Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need TList Example

Posted on 1998-12-28
10
Medium Priority
?
369 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
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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