Solved

List of custom Objects in Delphi

Posted on 2014-02-23
4
1,049 Views
Last Modified: 2014-02-24
I wish to create a TList of records for example

type TLayer = record  
   Name : string ;
   Line_Color : TColor ;  
   Visible : boolean ;
end ;

so I can Add and Delete items in the list (an Item being one record).
I have tried TObjectList as per following code, but cannot access the data.
Do I need to typecast a TObject back to TLayer somehow or should I be using TList instead of TObject List ?

Also I need help with constructors and destructors for a record and the list.

// Create a TLayer Class
//
type
   TLayer = class(TObject)

   private
      Name       : string ;
      Line_Color : TColor ;  
      Visible    : boolean ;
   public
      property aName       : String  read Name       write Name ;
      property aLine_Color : TColor  read Line_Color write line_Color ;
      property aVisible    : Boolean read Visible    write Visible ;
   end ;

.....
var
aLayer : TLayer ;
Layers : TObjectList ;

......

procedure TForm1.Create_Default_Layers_List ;
var
   s80 : string ;
begin
   // Setup a List of TLayer Objects
   //
   Layers := TObjectList.Create ; // Zero based array
                                                   // Destroy at Form1.Close
   Layers.Clear ;
 
   aLayer := TLayer.Create ;

   with aLayer do begin
      Name := 'All Layers' ;
      Line_Color := clBlack ;
      Visible := True ;
   end ;
   Layers.Add(aLayer) ;
......

// I have trouble trying to access the Items properties here, for example
  
 s80 := Layers.Items[0].Name ;

Open in new window

0
Comment
Question by:magman5
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39881794
You can use TList too. And yes - you need to do typecast.
MyLayer := TLayer(Layers.Items[0]);
... MyLayer.Name ...
....

Open in new window

Little advantage is on TObjectList side. TObjectList use your object destroy procedure to release memory - with TList you must do it. But more memory is allocated for each TLayer with TObjectList (TLayer as TObject).
0
 
LVL 19

Expert Comment

by:Thommy
ID: 39881802
Use TObjectList this way (Delphi XE5)...
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,System.Generics.Collections, Vcl.StdCtrls;

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

  TLayer = class(TObject)
  private
    Name       : string;
    Line_Color : TColor;
    Visible    : boolean;
  public
    property aName       : String  read Name       write Name;
    property aLine_Color : TColor  read Line_Color write line_Color;
    property aVisible    : Boolean read Visible    write Visible;
  end ;


var
  Form1: TForm1;
  aLayer: TLayer;
  Layers: TObjectlist<TLayer>;


implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
  Create_Default_Layers_List;
end;

procedure TForm1.Create_Default_Layers_List;
var
   s80 : string ;
begin
   // Setup a List of TLayer Objects
   //
  Layers := TObjectList<TLayer>.Create; // Zero based array
                                                   // Destroy at Form1.Close
  Layers.Clear ;

  aLayer := TLayer.Create ;

   with aLayer do begin
      Name := 'All Layers' ;
      Line_Color := clBlack ;
      Visible := True ;
   end ;
   Layers.Add(aLayer) ;

// I have trouble trying to access the Items properties here, for example

    s80 := Layers.Items[0].Name ;
   ShowMessage(s80);
   Layers.Free;
end;

end.

Open in new window

0
 
LVL 19

Accepted Solution

by:
Thommy earned 335 total points
ID: 39881827
Typecasting is necessary.
This is the safer way with runtime checking...

s80 := (Layers.Items[0] as TLayer).Name

Read chapter "Play Safe" at Delphi Type Casting
0
 

Author Comment

by:magman5
ID: 39882872
Thanks Thommy - this works well and solved my problem.

 Thanks also to Sinisa Vuk
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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