Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, Forms, Controls, Graphics, StdCtrls;
- type
- TMyListItem = TObject;
- TMyItemArray = array of TMyListItem;
- { TMyList }
- TMyList = class // (inherits from TObject)
- private
- FItems: TMyItemArray;
- FCount: integer;
- public
- // I would insert constructor here, but FCount starts 0 by default and
- // nothing else to initialize, so don't need to override constructor now.
- destructor Destroy; override;
- property Count: integer read FCount;
- property items: TMyItemArray read FItems;
- procedure Add(newItem: TMyListItem);
- procedure Clear;
- procedure Delete(index: integer);
- procedure Insert(newItem: TMyListItem; toIndex: integer);
- end;
- TMyList2 = class(TMyList);
- { TForm1 }
- TForm1 = class(TForm)
- btnAddRandom: TButton;
- Memo1: TMemo;
- procedure btnAddRandomClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- myList: TMyList;
- public
- procedure ShowMyList;
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.lfm}
- { TMyList }
- destructor TMyList.Destroy;
- begin
- Clear;
- inherited Destroy;
- end;
- procedure TMyList.Add(newItem: TMyListItem);
- begin
- inc(FCount);
- setlength(FItems, FCount);
- FItems[FCount-1]:=newItem;
- end;
- procedure TMyList.Clear;
- var i: integer;
- begin
- for i:=0 to Count-1 do
- if items[i]<>nil then
- items[i].Free;
- FCount:=0;
- setlength(FItems, 0);
- end;
- procedure TMyList.Delete(index: integer);
- var i: integer;
- begin
- if (index>=0) and (index<FCount) then begin
- if items[index]<>nil then
- items[index].Free;
- for i:=index to FCount-2 do
- FItems[i]:=FItems[i+1];
- dec(FCount);
- setlength(FItems, FCount);
- end;
- end;
- procedure TMyList.Insert(newItem: TMyListItem; toIndex: integer);
- var i: integer;
- begin
- inc(FCount);
- setlength(FItems, FCount);
- for i:=FCount-1 downto toIndex+1 do
- FItems[i]:=FItems[i-1];
- FItems[toIndex]:=newItem;
- end;
- { TForm1 }
- procedure TForm1.btnAddRandomClick(Sender: TObject);
- var n: integer;
- begin
- n:=random(4);
- case n of
- 0: myList.Add(TObject.Create);
- 1: myList.Add(TMyList2.Create);
- 2: myList.Add(TBitmap.Create);
- else myList.Add(TStringList.Create);
- end;
- ShowMyList;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- myList:=TMyList.Create;
- Randomize;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- myList.Free;
- end;
- procedure TForm1.ShowMyList;
- var i: integer;
- begin
- memo1.Lines.Clear;
- for i:=0 to myList.Count-1 do
- memo1.Lines.Add(myList.items[i].ClassName);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment