Solved

Need TList Example

Posted on 1998-12-28
10
359 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now