Solved

Calling methods within a TList

Posted on 2000-03-29
3
205 Views
Last Modified: 2010-04-04
My TList contains a list of TTimeRecord objects, the TTimeRecord objects contain sevgeral properties and methods (Read and write). What I want to be able to do is call the methods and access the properties of a particular item in the TList by calling the TList such as:

TList.Items[3].read;

I know I can create an instance of the TTime Record and point this to the item in the TList and call it that way but this seems messy and I'm sure you should be able to do it directly in the same way as you would call an array.

Of course the TList doesn't know it's a list of TTimeRecord so this me be the problem.

Any ideas how to do this?
0
Comment
Question by:mike_tipping
3 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 2666408
Try this:

type
  TMyRecord = record
                f: function: Integer;
                .....
              end;

function f1: Integer;
begin
  Result := 24;
end;

procedure TForm1.Button1Click(Sender: TObject);
var r: TMyRecord;
    l: TList;
begin
  r.f := f1;
  l := TList.Create;
  l.Add(@r);
  ShowMessage(IntToStr(TMyRecord(l.Items[0]^).f));
  l.Free;
end;
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2667541
I think he means this: (Borrowing Epsylons code...)

type
  TMyRecord = class(TObject)
                f: function: Integer;
                .....
              end;

procedure TForm1.Button1Click(Sender: TObject);
var r: TMyRecord;
    l: TList;
begin
  l := TList.Create;
  l.Add(TMyRecord.Create);
  ShowMessage(IntToStr(TMyRecord(l[0]).f));
  l.Free;
end;

You can make this slicker again by creating your own list desencdent like this:

  TMyList = class(TObjectList)
    protected
      Function Get(Index : Integer) : TMyRecord;
      procedure Put(Index: Integer; const Value: TMyRecord);

    public
      property Items[Index : Integer] : TMyRecord read Get write Put;
  end;


function TMyList.Get(Index: Integer): TMyRecord;
begin
  Result := Inherited Get(Index);
end;


procedure TMyList.Put(Index: Integer;
                                       const Value: TMyRecord);
begin
  Inherited Put(Index, Value);
end;


Then Button1Click looks like this:

procedure TForm1.Button1Click(Sender: TObject);
var r: TMyRecord;
    l: TMyList;
begin
  l := TMyList.Create;
  l.Add(TMyRecord.Create);
  ShowMessage(IntToStr(l[0].f));
  l.Free;
end;

Cheers,

Raymond.

0
 

Author Comment

by:mike_tipping
ID: 2707311
Sorry, but I think Epsylon gave me the answer I wanted first
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

813 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

14 Experts available now in Live!

Get 1:1 Help Now