Guest User

Object List example

a guest
Mar 26th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.70 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, StdCtrls;
  9.  
  10. type
  11.   TMyListItem = TObject;
  12.   TMyItemArray = array of TMyListItem;
  13.  
  14.   { TMyList }
  15.  
  16.   TMyList = class // (inherits from TObject)
  17.   private
  18.     FItems: TMyItemArray;
  19.     FCount: integer;
  20.   public
  21.     // I would insert constructor here, but FCount starts 0 by default and
  22.     // nothing else to initialize, so don't need to override constructor now.
  23.     destructor Destroy; override;
  24.     property Count: integer read FCount;
  25.     property items: TMyItemArray read FItems;
  26.     procedure Add(newItem: TMyListItem);
  27.     procedure Clear;
  28.     procedure Delete(index: integer);
  29.     procedure Insert(newItem: TMyListItem; toIndex: integer);
  30.   end;
  31.  
  32.   TMyList2 = class(TMyList);
  33.  
  34.   { TForm1 }
  35.  
  36.   TForm1 = class(TForm)
  37.     btnAddRandom: TButton;
  38.     Memo1: TMemo;
  39.     procedure btnAddRandomClick(Sender: TObject);
  40.     procedure FormCreate(Sender: TObject);
  41.     procedure FormDestroy(Sender: TObject);
  42.   private
  43.     myList: TMyList;
  44.   public
  45.     procedure ShowMyList;
  46.   end;
  47.  
  48. var
  49.   Form1: TForm1;
  50.  
  51. implementation
  52.  
  53. {$R *.lfm}
  54.  
  55. { TMyList }
  56.  
  57. destructor TMyList.Destroy;
  58. begin
  59.   Clear;
  60.   inherited Destroy;
  61. end;
  62.  
  63. procedure TMyList.Add(newItem: TMyListItem);
  64. begin
  65.   inc(FCount);
  66.   setlength(FItems, FCount);
  67.   FItems[FCount-1]:=newItem;
  68. end;
  69.  
  70. procedure TMyList.Clear;
  71. var i: integer;
  72. begin
  73.   for i:=0 to Count-1 do
  74.     if items[i]<>nil then
  75.       items[i].Free;
  76.   FCount:=0;
  77.   setlength(FItems, 0);
  78. end;
  79.  
  80. procedure TMyList.Delete(index: integer);
  81. var i: integer;
  82. begin
  83.   if (index>=0) and (index<FCount) then begin
  84.     if items[index]<>nil then
  85.       items[index].Free;
  86.     for i:=index to FCount-2 do
  87.       FItems[i]:=FItems[i+1];
  88.     dec(FCount);
  89.     setlength(FItems, FCount);
  90.   end;
  91. end;
  92.  
  93. procedure TMyList.Insert(newItem: TMyListItem; toIndex: integer);
  94. var i: integer;
  95. begin
  96.   inc(FCount);
  97.   setlength(FItems, FCount);
  98.   for i:=FCount-1 downto toIndex+1 do
  99.     FItems[i]:=FItems[i-1];
  100.   FItems[toIndex]:=newItem;
  101. end;
  102.  
  103. { TForm1 }
  104.  
  105. procedure TForm1.btnAddRandomClick(Sender: TObject);
  106. var n: integer;
  107. begin
  108.   n:=random(4);
  109.   case n of
  110.     0: myList.Add(TObject.Create);
  111.     1: myList.Add(TMyList2.Create);
  112.     2: myList.Add(TBitmap.Create);
  113.     else myList.Add(TStringList.Create);
  114.   end;
  115.   ShowMyList;
  116. end;
  117.  
  118. procedure TForm1.FormCreate(Sender: TObject);
  119. begin
  120.   myList:=TMyList.Create;
  121.   Randomize;
  122. end;
  123.  
  124. procedure TForm1.FormDestroy(Sender: TObject);
  125. begin
  126.   myList.Free;
  127. end;
  128.  
  129. procedure TForm1.ShowMyList;
  130. var i: integer;
  131. begin
  132.   memo1.Lines.Clear;
  133.   for i:=0 to myList.Count-1 do
  134.     memo1.Lines.Add(myList.items[i].ClassName);
  135. end;
  136.  
  137. end.
Advertisement
Add Comment
Please, Sign In to add comment