Solved

List of custom Objects in Delphi

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

10 Experts available now in Live!

Get 1:1 Help Now