Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TList Objects

Posted on 2004-11-18
6
Medium Priority
?
240 Views
Last Modified: 2013-11-23
say I declare an object type like..

TMyObject = class(TObject);
  Name : String;
  Address : String;
end;

and a List..

MyList : TList;

how can I set it up so that I can do something like MyList.Items[0].Username := 'Me';

instead of TMyObject(MyList.Items[0]).Username := 'Me';
0
Comment
Question by:rbohac
  • 3
  • 2
6 Comments
 
LVL 6

Author Comment

by:rbohac
ID: 12619141
oops. name, not username. ohh well..
0
 

Expert Comment

by:Drunkard_Englishman
ID: 12619312
you would need to inherit from the list, and create a new class which returns objects rather than pointers.

this would mean altering the items property and dereferencing your class inside the functions used by it before returning the value.
0
 
LVL 6

Author Comment

by:rbohac
ID: 12619383
can you show me an example?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
Drunkard_Englishman earned 500 total points
ID: 12619740
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TMyObject = class(TObject)
    Name : String;
    Address : String;
  end;

  TMyList = class(TList)
  protected
    function GetObject(Index: Integer): TMyObject;
    procedure PutObject(Index: Integer; Item: TMyObject);
  public
    function Add(Item: TMyObject): Integer;
    property Capacity;
    property Count;
    property Items[Index: Integer]: TMyObject read GetObject write PutObject; default;
    property List;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TMyList.GetObject(Index: Integer): TMyObject;
begin
  Result := TMyObject(inherited Items[Index]);
end;

procedure TMyList.PutObject(Index: Integer; Item: TMyObject);
begin
  inherited Items[Index] := Pointer(Item);
end;

function TMyList.Add(Item: TMyObject): Integer;
begin
  Result := inherited Add(Pointer(Item));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  obj: TMyObject;
  List: TMyList;
begin
  List := TMyList.Create;
  obj := TMyObject.Create;
  List.Add(obj);
  List[0].Name := 'gah';
  Caption := List[0].Name;
  List[0].Free;
  List.Free;
end;

end.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12627119
Instead of a TList, what about a dynamic array?

type
  TMyObjectList = array of TMyObject;

Unless you also need to sort your list, a dynamic array can be as powerfil as a TList.
0
 
LVL 6

Author Comment

by:rbohac
ID: 12773712
Perfect
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

864 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