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

Accessing an Object and its Data - Delphi OOP Part 4 / Chapter 9
Specifying a property

By Zarko Gajic, About.com

Specifying a property

Method-based property mapping is specified as follows:
...
TThisClass = class (TObject)
private
  FData: type;
  procedure SetData (const AData: type) ;
  function GetData: type;
public
  property Data: type read GetData write SetData;

...

function TThisClass.GetData: type;
begin
  // Validation, translation, etc, as needed
  Result := FData;
end;

procedure TThisClass.SetData(const AData: type) ;
begin
  // Validation, translation, etc, as needed
  FData := AData;
end;
When a program assigns a value to a property, this value is transferred as the AData parameter to the Set procedure (method). The Set method can process this parameter in various ways before assigning a value to the FData variable. FData has private scope to ensure that access to the value of FData is strictly controlled.

When a program uses a property’s value, this value is transferred as the return value of the Get function (method). The Get can perform whatever intermediate actions are needed and assigns a value derived from the FData variable to the function’s Result variable in order to return the value. The data types of the property and the underlying data field are usually the same but can differ if necessary.

Direct property mapping is specified as follows:

...
private
  FData: type;
public
  property Data: type read FData write FData;
...
When a program assigns a value to a directly-mapped property, this value is transferred directly to the private FData variable. When a program uses a property’s value, the value of FData variable is transferred directly without intermediate processing. No validation or manipulation is possible and the data types of the property and the underlying data field are the same and cannot differ.

If only a read mapping is specified (eg with constructed values), the property is readonly, and vice versa, although the object itself can still both read and write to the underlying data field.

Delphi provides a facility called Class Completion that can save a lot of typing in specifying properties (and in generating method skeletons): refer to online Help for details.

Synopsis of properties

This synopsis summarises what we have seen already in this chapter and mentions a number of points that will become clearer as we move through the module.
  • Properties can be accessed by mapping methods or by direct access.
  • A property can represent a (private) underlying data attribute or a constructed or modified value.
  • Properties can be Read/Write, Read only, or Write only.
  • Where appropriate, direct access saves coding. If necessary, direct access properties can later be extended to mapped access (by writing access methods) without affecting those parts of the program that already use the properties.
  • Mapping methods can be used to construct or modify data, to perform type conversion, and to provide access control and validation whenever the property is accessed.
  • Constructed values require a read access method to construct the value and typically do not have write access. (We’ll see this later.)
  • A superclass’s properties become part of the subclass definition in the same way that conventional data fields and methods do.
Next time: Object names as references...
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. Accessing an Object and its Data - Delphi OOP Part 4 / Chapter 9

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

All rights reserved.