?
Solved

Calling methods within a TList

Posted on 2000-03-29
3
Medium Priority
?
211 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 300 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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