?
Solved

List of custom Objects in Delphi

Posted on 2014-02-23
4
Medium Priority
?
1,288 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
[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
  • 2
4 Comments
 
LVL 27

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 1005 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

Technology Partners: 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!

Question has a verified solution.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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