home *** CD-ROM | disk | FTP | other *** search
- unit Obunit;
- { PC Plus sample Delphi program.
- Illustrates the basic techniques for declaring a class and
- constructing and destroying objects }
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TForm1 = class(TForm)
- ListBox1: TListBox;
- BookEd: TEdit;
- AuthorEd: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- AddBtn: TButton;
- ClearBtn: TButton;
- ExitBtn: TButton;
- ShowBtn: TButton;
- procedure ExitBtnClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure AddBtnClick(Sender: TObject);
- procedure ShowBtnClick(Sender: TObject);
- procedure ClearBtnClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure ShowObList;
- procedure FreeObs;
- end;
-
- { declare a fixed-length string }
- bkstr = string[50];
-
- { declare a BookOb class }
- BookOb = class(TObject)
- name : bkstr;
- author : bkstr;
- function ID : bkstr;
- end;
-
- var
- Form1: TForm1;
- ObList : TList; { declare a TList to hold our objects }
-
- implementation
-
- {$R *.DFM}
-
- function BookOb.ID : bkstr;
- begin
- ID := name + ' by ' + author;
- end;
-
- procedure TForm1.ShowObList;
- { Show each ob's ID (its name and author) in the ListBox }
- var
- i :integer;
- begin
- ListBox1.Clear;
- if ObList.Count = 0 then
- ListBox1.Items.Add( 'No books in the list!' )
- else
- for i := 0 to ObList.Count - 1 do
- ListBox1.Items.Add( BookOb(ObList.Items[i]).ID );
- end;
-
- procedure TForm1.FreeObs;
- { Free the objects in the ObList.
- ------------------------------
- This first frees all the objects in the ObList.
- Then is Clears the ObList.
- You must do both. If you Clear the ObList but don't Free the
- individual objects, they will hang around uselessly in memory.
- If you Free the objects but don't clear the ObList, the ObList
- will still 'think' it contains valid objects (which it doesn't,
- since they've been freed) }
- var
- i : integer;
- begin
- for i := 0 to ObList.Count - 1 do
- if ObList.Items[i] <> nil then
- BookOb(ObList.Items[i]).Free;
- ObList.Clear;
- ShowObList;
- end;
-
- procedure TForm1.ExitBtnClick(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- { Create a TList called ObList when the main form is created }
- ObList := TList.Create;
- end;
-
- procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- FreeObs; { Free the objects in the ObList, then... }
- ObList.Free; { Free the ObList when the main form is closed }
- end;
-
- procedure TForm1.AddBtnClick(Sender: TObject);
- var
- book : BookOb;
- begin
- if ((BookEd.Text = '') or (AuthorEd.Text = '')) then
- MessageDlg('You must enter a book and an author!', mtInformation,
- [mbOk], 0)
- else
- begin
- book := BookOb.Create;
- book.name := BookEd.Text;
- book.author := AuthorEd.Text;
- ObList.Add( book );
- ShowObList; { display the list of objects }
- end;
- end;
-
- procedure TForm1.ShowBtnClick(Sender: TObject);
- begin
- ShowObList;
- end;
-
- procedure TForm1.ClearBtnClick(Sender: TObject);
- begin
- FreeObs;
- ShowObList;
- end;
-
- end.
-