You are on page 1of 7

Chapter 1

Introduction to OO basics
Problems

Problem 1.1 Study Chapter 1


Identify the appropriate example(s) or section(s) of the chapter to illustrate each comment made in the summary above.

Problem 1.2 Replace exception handling with If statements


Replace all the exception handling statements in example 1.3 step 7 with If statements.

23 24 25 26 27 28 29 30 31 32

procedure TfrmMain.radAuxShowClick(Sender: TObject); begin if frmAuxiliary = nil then begin ShowMessage ('Auxiliary Form does not exist'); radAuxShow.Checked := False; end else frmAuxiliary.Show; end; // end procedure TfrmMain.radAuxShowClick

Introduction to OO basics (31 May 2006, all rights reserved)

Chapter 1, Page 1

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

procedure TfrmMain.radAuxHideClick(Sender: TObject); begin if frmAuxiliary = nil then begin ShowMessage ('Auxiliary Form does not exist'); radAuxHide.Checked := False; end else frmAuxiliary.Hide; end; // end procedure TfrmMain.radAuxHideClick procedure TfrmMain.btnCreateClick(Sender: TObject); begin if frmAuxiliary = nil then frmAuxiliary := TfrmAuxiliary.Create(Self) else ShowMessage ('Auxiliary Form already exists'); end; // end procedure TfrmMain.btnCreateClick procedure TfrmMain.btnFreeClick(Sender: TObject); begin if frmAuxiliary = nil then ShowMessage ('Auxiliary Form does not exist') else begin frmAuxiliary.Hide; frmAuxiliary.Free; frmAuxiliary := nil; end; end; // end procedure TfrmMain.btnFreeClick

Problem 1.3 Changed interface


The user interface given for example 1.3 step 7 mixes buttons and RadioButtons, leading to a confused user interface and to repetitive coding. Recode this program, replacing the various buttons with a single RadioGroup (figure 20 and figure 21). Use exception handling rather than If statements to respond to object reference errors.
Figure 20 An improved user interface for demonstrating object creation, use and destruction

Figure 21 Components comprising the user interface

Chapter 1, Page 2

Object orientation with Delphi (all rights reserved)

procedure TfrmMain.rgpAuxFormClick(Sender: TObject); begin try case rgpAuxForm.ItemIndex of 0: if frmAuxiliary = nil then begin frmAuxiliary := TfrmAuxiliary.Create(Self); ShowMessage ('Auxiliary Form created'); // optional extra end else ShowMessage ('Auxiliary Form already exists'); 1: frmAuxiliary.Show; 2: frmAuxiliary.Hide; 3: begin frmAuxiliary.Hide; frmAuxiliary.Free; frmAuxiliary := nil; ShowMessage ('Auxiliary Form freed'); // optional extra end; else ShowMessage ('Unrecognised operation'); end; except ShowMessage ('Auxiliary Form does not exist'); end; end; // end procedure TfrmMain.rgpAuxFormClick procedure TfrmMain.frmAuxiliaryShow(Sender: TObject); begin frmAuxiliary.Left := Random (600); frmAuxiliary.Top := Random (400); end; // end procedure TfrmAuxiliary.FormShow procedure TfrmMain.btnBlueClick(Sender: TObject); begin frmAuxiliary.Color := clSkyBlue; end; // end procedure TfrmMain.btnBlueClick procedure TfrmMain.btnGreenClick(Sender: TObject); begin ((Sender as TButton).Parent as TForm).Color := clMoneyGreen; end; // end procedure TfrmMain.btnGreenClick initialization Randomize; end. // end unit MainForm

Introduction to OO basics (31 May 2006, all rights reserved)

Chapter 1, Page 3

Problem 1.4 Auto-create and program controlled create and free


In example 1.3 step 7 we remove frmAuxiliary from the auto-create list and then create and free it under program control. What happens if we leave frmAuxiliary in the auto-create list but still use the revised program given in example 1.3 step 7?

The start up behaviour changes. If our first action is to click on the Create button there is an exception stating that the Form already exists. We can still go on to Show, Hide, Free and Create the auxiliary form as before.

Problem 1.5 Creating objects explicitly


If you have coded problem 1.3 successfully, use it as a starting point for this problem. Modify it to define the auxiliary form in code similarly to example 1.4. If you have not coded problem 1.3 successfully, use example 1.4 as a starting point. 1. On the auxiliary form define two buttons, btnBlue and btnGreen, in code (ie not using RAD facilities). Give each an OnClick event handler to change the colour of the auxiliary form between MoneyGreen and SkyBlue.

Well start from problem 1.3, which has two units, one for each of the RAD forms. Since well now create frmAuxiliary directly in program code we no longer need the second unit. By removing the second unit, we also remove the declarations for frmAuxiliary and its two buttons. So we need to declare these and the event handlers as part of TfrmMain in unit MainForm:
type TfrmMain = class(TForm) rgpAuxForm: TRadioGroup; procedure rgpAuxFormClick(Sender: TObject); private // now declared by the programmer frmAuxiliary: TForm; // data fields btnBlue, btnGreen: TButton; procedure frmAuxiliaryShow(Sender: TObject); // event handlers procedure btnBlueClick(Sender: TObject); procedure btnGreenClick(Sender: TObject); end; // end TfrmMain = class(TForm)

Chapter 1, Page 4

Object orientation with Delphi (all rights reserved)

The matching method definitions are as follows:


implementation {uses AuxForm; // no longer needed } {$R *.dfm} procedure TfrmMain.rgpAuxFormClick(Sender: TObject); begin try case rgpAuxForm.ItemIndex of 0: if frmAuxiliary = nil then begin frmAuxiliary := TForm.Create(Self); // assign an owner // dont assign a parent; defaults to the application frmAuxiliary.Caption := 'frmAuxiliary'; frmAuxiliary.Height := 200; frmAuxiliary.Width := 380; frmAuxiliary.OnShow := frmAuxiliaryShow; btnBlue := TButton.Create(frmAuxiliary); btnBlue.Parent := frmAuxiliary; btnBlue.Left := 50; btnBlue.Top := 50; btnBlue.Caption := 'Blue'; btnBlue.OnClick := btnBlueClick; btnGreen := TButton.Create(frmAuxiliary); btnGreen.Parent := frmAuxiliary; btnGreen.Left := 250; btnGreen.Top := 50; btnGreen.Caption := 'Green'; btnGreen.OnClick := btnGreenClick; { Announce successful Create} ShowMessage ('Auxiliary Form created'); end else ShowMessage ('Auxiliary Form already exists'); frmAuxiliary.Show; frmAuxiliary.Hide; begin frmAuxiliary.Hide; btnBlue.Free; btnBlue := nil; FreeAndNil (btnGreen); // hint in chapter 1 FreeAndNil (frmAuxiliary);

1: 2: 3:

{ Announce successful Free} ShowMessage ('Auxiliary Form freed'); end; else ShowMessage ('Unrecognised operation');

Introduction to OO basics (31 May 2006, all rights reserved)

Chapter 1, Page 5

end; except ShowMessage ('Auxiliary Form does not exist'); end; end; // end procedure TfrmMain.rgpAuxFormClick procedure TfrmMain.frmAuxiliaryShow(Sender: TObject); begin frmAuxiliary.Left := Random (600); frmAuxiliary.Top := Random (400); end; // end procedure TfrmAuxiliary.FormShow procedure TfrmMain.btnBlueClick(Sender: TObject); begin frmAuxiliary.Color := clSkyBlue; end; // end procedure TfrmMain.btnBlueClick procedure TfrmMain.btnGreenClick(Sender: TObject); begin ((Sender as TButton).Parent as TForm).Color := clMoneyGreen; end; // end procedure TfrmMain.btnGreenClick initialization Randomize; end. // end unit MainForm

Notice that we do not assign a parent for frmAuxiliary, and so by default the Application becomes the parent. If we were to assign frmMain as the parent, frm Auxiliary would appear on frmMain and not as a separate form. 2. Draw the matching UML diagrams, showing composition explicitly. Class diagram: One needs to think a bit about this. If we refer to the code, we notice that we define only one class, TfrmMain, and then create several objects (frmAuxiliary, btnBlue and btnGreen) as part of this class. So, we have one class that carries references to several objects of various classes.

Chapter 1, Page 6

Object orientation with Delphi (all rights reserved)

Object diagram: As part of the RAD process, frmMain automatically creates rgpAuxForm. As part of rgpAuxFormClick it creates frmAuxiliary, btnBlue and btnGreen.

Sequence diagram: There are a number of possible user actions, of which we show two in the following diagram:

Introduction to OO basics (31 May 2006, all rights reserved)

Chapter 1, Page 7

You might also like