Solved

Calling methods within a TList

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

726 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