1. Home
  2. Computing & Technology
  3. Delphi Programming

Programmer Defined Classes and Objects - Delphi OOP Part 3 / Chapter 6
Using your Custom Class

By Zarko Gajic, About.com

23 implementation

24 {$R *.dfm}

25 uses
26    ItemU;

27 var
28    ItemCount: TItem;

29 procedure TfrmCount.btnAddItemClick(Sender: TObject) ;
30 begin
31    ItemCount.AddItem;
32 end;

33 procedure TfrmCount.btnDisplayMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
35 begin
36    lblTotal.Caption := IntToStr(ItemCount.GetCount) ;
37 end;

38 procedure TfrmCount.btnDisplayMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
40 begin
41    lblTotal.Caption := '';
42 end;

43 procedure TfrmCount.bmbResetClick(Sender: TObject) ;
44 begin
45    ItemCount.ZeroCount;
46 end;

47 initialization
48    ItemCount := TItem.Create;

49 finalization
50    ItemCount.Free;

end.
The constructor TItem.Create, called in line 48, sets aside the memory to hold the object, initialises the object’s data fields, and returns the address of the object just created. So we typically create the object and assign the return value to the name we have already declared (the reference) in a single program step. This creates both the object and the link between the object name and the object itself:

ItemCount := TItem.Create;

Notice that we create (instantiate) an object from a class. So when creating an object we call the class’s Create method (TItem.Create, line 48) and assign that to the object name declared in the variable declaration (line 28). When using an object, once it has been created, the method calls are to that object (ItemCount.AddItem in line 31, ItemCount.GetCount in line 36 and ItemCount.ZeroCount in line 45, and not TItem.AddItem etc).

You always have to make sure that you free the objects you create. The ItemCount object is freed in the finalization section of the unit.

Save all the code from example 3.2 as we’ll elaborate further on it in exs 3.3 and 3.4. Run and test this program.

If you want to trace what is happening in this program, return to the editor and place breakpoints in the bodies of the OnClick event handlers (lines 31, 36 and 45 in OODriverU.pas). (To place a breakpoint, place the cursor on the specific line in the editor and then press <F5>.) Run the program again. Click on the different buttons, step through the program line by line by using <F7> and notice how execution moves between units. Resume normal operation through <F9>. (<F5> is a toggle command and will also remove a breakpoint.)

That's it for today, next time we'll take a look at subclassing and inheritance (Chapter 7).

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Programmer Defined Classes and Objects - Delphi OOP Part 3 / Chapter 6

©2009 About.com, a part of The New York Times Company.

All rights reserved.