Solved

Calling methods within a TList

Posted on 2000-03-29
3
203 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

19 Experts available now in Live!

Get 1:1 Help Now