delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (4)


Using TList's and Pointers in delphiFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
OO-related
Skill Level:
Scoring:
Last Update:
06/20/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet Tlist pointers helper-class
Times Scored:
4
Visits:
7893
Uploader: Stewart Moss
Company: New Heights Software Developme
Reference: N/A
 
Question/Problem/Abstract:
This was a small demonstration to teach a friend how to use the TList helper object.
  
Also gives you an idea on how to use pointers.
  
It creates a list of Pointers to Integers but they could be a list of pointers  to any record or class type.
Answer:




{-----------------------------------------------------------------------------
Unit Name:     Unit1
Author:        StewartM (Stewart Moss)

Creation Date: 20, 01, 2003  (13:02,)
Documentation Date: 20, 01, 2003  (13:02,)

Version 1.0
-----------------------------------------------------------------------------

Compiler Directives:

Purpose:

Dependancies:

Description:

  This was written by Stewart Moss
  This was a small demonstration to teach a friend how to use the TList helper
  object.
  
  Also gives you an idea on how to use pointers.
  
  It creates a list of Pointers to Integers but they could be a list of pointers
  to any record or class type.
  
  At the end of this is the source for the DFM file used to run this application.
  (It just needs to be bound into a project)
  

Notes:

History:

Copyright 2003 by Stewart Moss. All rights reserved.
-----------------------------------------------------------------------------}

unit Unit1;

interface

uses
  
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  
Dialogs, StdCtrls;

type

  
PInteger = ^Integer;

  
TForm1 = class(TForm)
    
btnAdd: TButton;
    
btnDelete: TButton;
    
ListBox1: TListBox;
    
Button1: TButton;
    
procedure FormCreate(Sender: TObject);
    
procedure FormClose(Sender: TObject; var Action: TCloseAction);
    
procedure btnAddClick(Sender: TObject);
    
procedure btnDeleteClick(Sender: TObject);
    
procedure Button1Click(Sender: TObject);
    
procedure CustomizeDlg1Close(Sender: TObject);
    
procedure acnTestExecute(Sender: TObject);
  
private
    
{ Private declarations }

    
TempList: TList;

    
procedure FreeList;
    
procedure showlist;

  
public
    
{ Public declarations }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  
TempList := TList.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  
FreeList;
  
TempList.Free;
end;

procedure TForm1.FreeList;
{-----------------------------------------------------------------------------
  Procedure: TForm1.FreeList
  Author:    Stewart Moss
  Date:      20-Jan-2003

    This disposes of all the existing items in the list.

    Remember by Deleting the first element in the list, you are shifting the
    list up and you are also reducing the TempList.count..

    ** Very important you can't just free the TList object and expect **
    ** everything else to disappear                                   **
-----------------------------------------------------------------------------}
var
  
loop: Integer;
  
tmpcount : Integer;
begin
  
tmpcount := templist.Count -1;

  
for loop := 0 to tmpcount do
  begin
    
dispose(TempList.Items[0]);
    
TempList.Delete(0);
  
end;

end;

procedure TForm1.btnAddClick(Sender: TObject);
var
  
tmpint: Integer;
  
AInteger: PInteger;
begin
  
Randomize;
  
tmpint := Random(1000);

  
// Create and assign memory to a new Integer and store it's pointer
  // in AInteger
  
new(AInteger);

  
// Now take tmpint and store it at the memory address referenced by the pointer
  // AInteger
  
AInteger^ := tmpint;

  
// Now add this pointer to the TList
  // This returns the index position of the item (ie where it is added to the list)
  
TempList.Add(AInteger);


  
showlist;
end;

procedure TForm1.showlist;
{-----------------------------------------------------------------------------
  Procedure: TForm1.showlist
  Author:    Stewart Moss
  Date:      20-Jan-2003

    PInteger(templist.Items[loop])^ returns the integer value stored at the
    Integer Pointer in the List (de-reference)
-----------------------------------------------------------------------------}
var
  
loop: Integer;
begin
  
ListBox1.Items.Clear;
  
for loop := 0 to TempList.Count - 1 do
  begin
    
ListBox1.Items.Add(IntToStr(loop) + ' -> ' + IntToStr(PInteger(TempList.Items[loop])^));
  
end;                                  // for
end;

procedure TForm1.btnDeleteClick(Sender: TObject);
var
  
tmpstr: string;
  
tmpint, tmpint2: Integer;
begin

  
// User has to enter the index of the item to delete (ie remove from the list)
  
tmpstr := Inputbox('Delete an item out of the list', 'Which item do you want to delete (0=first) ?', '');

  
if tmpstr = '' then
    
Exit;

  
try
    
tmpint := StrToInt(trim(tmpstr));
  
except
    raise
Exception.Create(tmpstr + ' is not an integer!');
  
end;

  
if tmpint > TempList.Count - 1 then
    raise
Exception.Create('Number too high!');

  
tmpint2 := Integer(TempList.Items[tmpint]^);
  
dispose(TempList.Items[tmpint]);
  
TempList.Delete(tmpint);

  
ShowMessage('Deleted Item Index ' + IntToStr(tmpint) + ' Value ' + IntToStr(tmpint2));

  
showlist;
end;

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



end.

(*

UNIT11.DFM
----------

object Form1: TForm1
  Left = 250
  Top = 221
  Width = 277
  Height = 401
  Caption = 'Stewart Moss TList Example'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnClose = FormClose
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object btnAdd: TButton
    Left = 24
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Add'
    TabOrder = 0
    OnClick = btnAddClick
  end
  object btnDelete: TButton
    Left = 104
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Delete'
    TabOrder = 1
    OnClick = btnDeleteClick
  end
  object ListBox1: TListBox
    Left = 28
    Top = 92
    Width = 185
    Height = 233
    ItemHeight = 13
    TabOrder = 2
  end
  object Button1: TButton
    Left = 184
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Refresh'
    TabOrder = 3
    OnClick = Button1Click
  end
end

*)








Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
better FreeList procedure
    Richard Winston (Jun 27 2003 3:54PM)

A better version of TForm1.FreeList is given below.  It is better than the original one because it does not require all the items in the list to be moved up one position in the list when that item is removed.  For large lists, the time required to move all the items can be significant.

procedure TForm1.FreeList;
var
  loop: Integer;
begin
  for loop := templist.Count -1 downto 0 do
  begin
    dispose(TempList.Items[0]);
  end;
  TempList.Clear
end;

Respond

RE: better FreeList procedure
Stewart Moss (Jun 29 2003 5:48PM)

Thank you very much for your tip!

I guess I have some classes to change now!!!


hehehe


Respond

PInteger
    Martin Strand (Jun 22 2003 8:02PM)

You doesn't need to declare PInteger, since it already is declared within some Delphi unit. The declaration is the same, though.
Respond

RE: PInteger
Stewart Moss (Jun 23 2003 6:30PM)

The declaration of PInteger was to show that you can use a pointer to anything. I will be sumitting a more complicated example shortly to demonstrate how to use pointers of other types (ie Records)
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)