In the previous example, we used Delphis RAD facility to define the class TfrmAuxiliary. We then instantiated and freed it in program code. As an alternative, in this example we define TfrmAuxiliary directly from TForm using program code and then instantiate it.
Ex 1.4 step 1 Modifying the previous program
In a spirit of reuse, and to emphasise the changes well be making in this example, open the project from the previous example and then save it (ie the two units and the project file) to a new directory. Well modify this copy. Since well define the auxiliary form directly from TForm (line 48 below) we dont need unit AuxForm of the previous example. So in unit MainForm delete the local uses AuxForm statement. Now we need to remove AuxForm from the project. Use the menu sequence Project | Remove from Project, select unit AuxForm and click on OK. (To keep your directory tidy, you could now also delete unit AuxForm from the directory you are using for this example.)Unit AuxForm declared a reference to frmAuxiliary. This unit is no longer part of the project and so we declare this reference as a private data field in the type declaration of TfrmMain (lines 1718 below). The reference frmAuxiliary is now a private data field in TfrmMain.
As in the previous example, if the object that frmAuxiliary refers to does not already exist (line 46) we create it, but this time we create it directly from the library class TForm (line 48). (In the previous example we created it from TfrmAuxiliary: example 1.3, step 4, line 44.) We now initialise it in program code (lines 4951) since we no longer have access to the Object Inspector to initialise frmAuxiliary. We also set the values of Left and Top for frmAuxiliary (lines 5253) since we dont have a suitable OnShow event handler (cf example 1.3, step 2, lines 1822). Showing, Hiding and Freeing frmAuxiliary remains the same process as before.
1 unit MainForm;When you run this program it appears on the screen much as the previous examples. So what is the significance of the changes we have made?
2 interface
3
4 uses
5 Windows, Messages, SysUtils, Variants, Classes, Graphics,
6 Controls, Forms, Dialogs, StdCtrls;
7 type
8 TfrmMain = class(TForm)
9 radAuxShow: TRadioButton;
10 radAuxHide: TRadioButton;
11 gpbAuxiliary: TGroupBox;
12 btnCreate: TButton;
13 btnFree: TButton;
14 procedure radAuxShowClick(Sender: TObject) ;
15 procedure radAuxHideClick(Sender: TObject) ;
16 procedure btnCreateClick(Sender: TObject) ;
17 procedure btnFreeClick(Sender: TObject) ;
18 private
19 frmAuxiliary: TForm; // Composition
20 end;
21 var
22 frmMain: TfrmMain;
23 implementation
24 {$R *.dfm}
25 procedure TfrmMain.radAuxShowClick(Sender: TObject) ;
26 begin
27 try
28 frmAuxiliary.Show;
29 except
30 ShowMessage ('Auxiliary Form does not exist') ;
31 radAuxShow.Checked := False;
32 radAuxHide.Checked := False;
33 end;
34 end;
35 procedure TfrmMain.radAuxHideClick(Sender: TObject) ;
36 begin
37 try
38 frmAuxiliary.Hide;
39 except
40 ShowMessage ('Auxiliary Form does not exist') ;
41 radAuxHide.Checked := False;
42 radAuxShow.Checked := False;
43 end;
44 end;
45 procedure TfrmMain.btnCreateClick(Sender: TObject) ;
46 begin
47 if frmAuxiliary = nil then
48 begin
49 frmAuxiliary := TForm.Create(nil) ;
50 frmAuxiliary.Caption := 'frmAuxiliary';
51 frmAuxiliary.Height := 150;
52 frmAuxiliary.Width := 180;
53 frmAuxiliary.Left := Random (600) ;
54 frmAuxiliary.Top := Random (400) ;
55 end
56 else
57 ShowMessage ('Auxiliary Form already exists') ;
58 end;
We declare frmAuxiliary (of class TForm) as a constituent of class TfrmMain by declaring it as a private data member (or data field) (line 18). We then create it (line 48), manipulate it (lines 4953) and, when we have finished with it, we free it (line 62). This private create-use-free relationship between one object and another is an example of composition. Its similar to association, but where association is a relationship between two independent objects, with composition one object creates, uses and destroys a private subsidiary object.
Composition is an important concept in OO and well return to it several times in the chapters that follow.

