Solved

List of custom Objects in Delphi

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can Viruses spread while transferring files via FTP protocol to unshared NAS drives ? 6 122
LAN or WAN ? 11 119
DBGrid or StringGrid ? 6 135
delphi popmenu non latine charcters 3 47
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

734 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