home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 12 / 12_pcplus_supercd.iso / Pcplus / Delphi / OBUNIT.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1997-04-10  |  3.3 KB  |  139 lines

  1. unit Obunit;
  2. { PC Plus sample Delphi program.
  3.   Illustrates the basic techniques for declaring a class and
  4.   constructing and destroying objects }
  5.  
  6. interface
  7.  
  8. uses
  9.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  10.   Forms, Dialogs, StdCtrls;
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     ListBox1: TListBox;
  15.     BookEd: TEdit;
  16.     AuthorEd: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     AddBtn: TButton;
  20.     ClearBtn: TButton;
  21.     ExitBtn: TButton;
  22.     ShowBtn: TButton;
  23.     procedure ExitBtnClick(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  26.     procedure AddBtnClick(Sender: TObject);
  27.     procedure ShowBtnClick(Sender: TObject);
  28.     procedure ClearBtnClick(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.   public
  32.     { Public declarations }
  33.     procedure ShowObList;
  34.     procedure FreeObs;
  35.   end;
  36.  
  37. { declare a fixed-length string }
  38.   bkstr = string[50];
  39.  
  40. { declare a BookOb class        }
  41.   BookOb = class(TObject)
  42.     name   : bkstr;
  43.     author : bkstr;
  44.     function ID : bkstr;
  45.   end;
  46.  
  47. var
  48.   Form1: TForm1;
  49.   ObList : TList; { declare a TList to hold our objects }
  50.  
  51. implementation
  52.  
  53. {$R *.DFM}
  54.  
  55. function BookOb.ID : bkstr;
  56. begin
  57.   ID := name + ' by ' + author;
  58. end;
  59.  
  60. procedure TForm1.ShowObList;
  61. { Show each ob's ID (its name and author) in the ListBox }
  62. var
  63.    i :integer;
  64. begin
  65.   ListBox1.Clear;
  66.   if ObList.Count = 0 then
  67.     ListBox1.Items.Add( 'No books in the list!' )
  68.   else
  69.   for i := 0 to ObList.Count - 1 do
  70.       ListBox1.Items.Add( BookOb(ObList.Items[i]).ID );
  71. end;
  72.  
  73. procedure TForm1.FreeObs;
  74. { Free the objects in the ObList.
  75.   ------------------------------
  76.   This first frees all the objects in the ObList.
  77.   Then is Clears the ObList.
  78.   You must do both. If you Clear the ObList but don't Free the
  79.   individual objects, they will hang around uselessly in memory.
  80.   If you Free the objects but don't clear the ObList, the ObList
  81.   will still 'think' it contains valid objects (which it doesn't,
  82.   since they've been freed) }
  83. var
  84.   i : integer;
  85. begin
  86.   for i := 0 to ObList.Count - 1 do
  87.       if ObList.Items[i] <> nil then
  88.          BookOb(ObList.Items[i]).Free;
  89.   ObList.Clear;
  90.   ShowObList;
  91. end;
  92.  
  93. procedure TForm1.ExitBtnClick(Sender: TObject);
  94. begin
  95.   Close;
  96. end;
  97.  
  98. procedure TForm1.FormCreate(Sender: TObject);
  99. begin
  100.   { Create a TList called ObList when the main form is created }
  101.   ObList := TList.Create;
  102. end;
  103.  
  104. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  105. begin
  106.   FreeObs;     { Free the objects in the ObList, then...      }
  107.   ObList.Free; { Free the ObList when the main form is closed }
  108. end;
  109.  
  110. procedure TForm1.AddBtnClick(Sender: TObject);
  111. var
  112.    book : BookOb;
  113. begin
  114.   if ((BookEd.Text = '') or (AuthorEd.Text = '')) then
  115.         MessageDlg('You must enter a book and an author!', mtInformation,
  116.       [mbOk], 0)
  117.   else
  118.   begin
  119.     book := BookOb.Create;
  120.     book.name := BookEd.Text;
  121.     book.author := AuthorEd.Text;
  122.     ObList.Add( book );
  123.     ShowObList; { display the list of objects }
  124.   end;
  125. end;
  126.  
  127. procedure TForm1.ShowBtnClick(Sender: TObject);
  128. begin
  129.   ShowObList;
  130. end;
  131.  
  132. procedure TForm1.ClearBtnClick(Sender: TObject);
  133. begin
  134.   FreeObs;
  135.   ShowObList;
  136. end;
  137.  
  138. end.
  139.