TList Objects

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';
LVL 6
rbohacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rbohacAuthor Commented:
oops. name, not username. ohh well..
0
Drunkard_EnglishmanCommented:
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
rbohacAuthor Commented:
can you show me an example?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Drunkard_EnglishmanCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wim ten BrinkSelf-employed developerCommented:
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
rbohacAuthor Commented:
Perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.