Ide Indy Delphi

Title: Using TList's and Pointers in delphi
Question: 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
*)