Marco Cantù 1998, Mastering Delphi 4
Project: DYNARR.DPR
Project Structure
DYNARR.DPR
program DynArr;
uses
Forms,
DynArrF in 'DynArrF.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
DYNARRF.PAS
unit DynArrF;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
btnFill: TButton;
btnAlias: TButton;
btnSet: TButton;
btnGrow: TButton;
btnGet: TButton;
procedure btnFillClick(Sender: TObject);
procedure btnAliasClick(Sender: TObject);
procedure btnSetClick(Sender: TObject);
procedure btnGrowClick(Sender: TObject);
procedure btnGetClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
Array1, Array2: array of Integer;
procedure TForm1.btnFillClick(Sender: TObject);
var
I: Integer;
begin
// fill
for I := Low (Array1) to High (Array1) do
Array1 [I] := I;
end;
procedure TForm1.btnSetClick(Sender: TObject);
begin
Array1 [99] := 100;
end;
procedure TForm1.btnGrowClick(Sender: TObject);
begin
// grow keeping existing values
SetLength (Array1, 200);
end;
procedure TForm1.btnGetClick(Sender: TObject);
begin
// extract
Caption := IntToStr (Array1 [99]);
end;
procedure TForm1.btnAliasClick(Sender: TObject);
begin
// alias
Array2 := Array1;
// change one (both change)
Array2 [99] := 1000;
// show the other
Caption := IntToStr (Array1 [99]);
if Array1 = Array2 then
Beep;
// truncate first array
Array1 := Copy (Array2, 0, 10);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// allocate
SetLength (Array1, 100);
end;
end.
DYNARRF.DFM
object Form1: TForm1
Left = 197
Top = 108
Width = 203
Height = 166
ActiveControl = btnFill
BorderWidth = 1
Caption = 'DynArr'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object btnFill: TButton
Left = 16
Top = 24
Width = 75
Height = 25
Caption = '&Fill'
TabOrder = 0
OnClick = btnFillClick
end
object btnAlias: TButton
Left = 16
Top = 104
Width = 161
Height = 25
Caption = '&Create alias and truncate'
TabOrder = 1
OnClick = btnAliasClick
end
object btnSet: TButton
Left = 104
Top = 24
Width = 75
Height = 25
Caption = '&Set value'
TabOrder = 2
OnClick = btnSetClick
end
object btnGrow: TButton
Left = 16
Top = 56
Width = 75
Height = 25
Caption = '&Grow'
TabOrder = 3
OnClick = btnGrowClick
end
object btnGet: TButton
Left = 104
Top = 56
Width = 75
Height = 25
Caption = 'Get &value'
TabOrder = 4
OnClick = btnGetClick
end
end
Copyright Marco Cantù 1998