Solved

List of custom Objects in Delphi

Posted on 2014-02-23
4
1,034 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now