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

Accessing an Object and its Data - Delphi OOP Part 4 / Chapter 10
Object names as references

By , About.com Guide

OOP Chapter 10: Using Objects
Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 9

Introducing (in this Chapter):

  • The concept of ‘Patterns’ in object orientation
  • The Immutable Pattern
  • Creating a list of object references

  • Homework Problems
  • Part download: PDF, Code, Homework Solutions

Example 4.6 Object names as references

So far in this chapter we have looked at ways of accessing objects’ data. In summary, this data is declared as private and so is encapsulated: we provide and control access through access methods and properties.

In this example we look at accessing the objects themselves. As we have already seen, an object’s name is a reference to the object. Whenever we use the name of an object, Delphi (and Java and C# too) automatically dereferences the name to get the object’s address. Often we don’t need to bother about the dereferencing. But it becomes important at times, for example when creating an association between two objects, and we look at some of the implications of a reference model in this example, which extends example 4.5. It creates a series of Client objects, adds a reference to each object to a list, in this case a TListBox component, and displays the contents of each object by dereferencing the matching item in the list.

You may wish either to edit the program from example 4.5. Otherwise, to start a new application, build the user interface, and add unit ClientU from example 4.5 to the new project.

Ex 4.6 step 1 Creating and adding objects to a list

We’ll use btnCreate’s OnClick event handler to create an object of type TClient (line 38 below) and then add a reference to this object to the ListBox (line 40).
unit ObjAccessU;

interface
   { Standard RAD interface declaration }

22 implementation

23 uses ClientU; // Move from global uses clause

24 {$R *.dfm}

25 {TfrmAccessObject}


26 procedure TfrmAccessObject.btnCreateClick(Sender: TObject) ;
27 var
28   NewClient: TClient;
29   Dummy: integer;
30 begin
31   // Exit if input invalid
32   if (edtCName.Text = '') or (not TryStrToInt(edtAccNo.Text, Dummy)) then
33   begin
34    ShowMessage ('Please enter a valid name and/or number') ;
35    Exit;
36   end;

37   NewClient := TClient.Create(edtCName.Text, edtAccNo.Text) ;

38   // Add object to ListBox and create an additional reference
39   lstClients.AddItem(NewClient.CName, NewClient) ;

40   edtCName.Clear;
41   edtAccNo.Clear;
42   edtCName.SetFocus;
43 end;
Note that the NewClient reference is no longer part of the user interface (as in example 4.1 step 2 lines 18–19) and is instead declared locally to the event handler (lines 27–28 above). This also means that the global uses clause reference to ClientU (example 4.1 step 2 line 5) can move to a local uses clause (line 23 above), thus restricting visibility into ClientU.

We mentioned earlier that TClient assumes that the initialisation values it receives for CName and AccNo are valid. The user interface therefore takes the responsibility of checking that the value entered by the user for the client’s name is not an empty string (line 32) and that the account number is a valid integer (line 33 – refer to online Help if you are unfamiliar with the TryIntToStr function).

Line 40 introduces a ListBox method, AddItem, that may be new. We often use the Add method to add a string to a ListBox. AddItem allows us to add a string and an associated object reference to a ListBox. If you consult the online Help you will see that it is declared as:

procedure AddItem(Item: String; AObject: TObject) ;

So in line 40 we add the client’s name as a string to the ListBox and we also add a reference to the object we have just created by passing the name we assigned in line 38. We’ll use this object reference in the next step. When this method terminates, it loses the ‘NewClient’ reference since this is a locally declared variable (lines 27–28). The only reference we now have to this object is the reference stored in the ListBox by line 40.

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Accessing an Object and its Data - Delphi OOP Part 4 / Chapter 10

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

All rights reserved.