Today I learned… :) I didn’t know about this syntax for properties… (via: Roman Yankovsky – Google+)
Posted by jpluimers on 2014/04/21
Interesting: Roman Yankovsky just wrote about a property syntax that I didn’t know about either:
Today I learned… :)
I didn’t know about this syntax for properties:
FStrings: array [0..1] of string; property String0: string read FStrings[0] write FStrings[0]; property String1: string read FStrings[1] write FStrings[1];
Now I hope someone finds out in which Delphi version this syntax was introduced.
–jeroen
via: Roman Yankovsky – Google+ – Today I learned… :) I didn’t know about this syntax for….






Lars Fosdal said
I suppose any static property field can be accessed directly without setter and getter methods?
jpluimers said
Need to try that as well.
Thaddy said
This has been the case since Delphi 2 AFAIK. Maybe even D1.
You overlooked that it is a simple reference, not a getter.
jpluimers said
Yup, and this “overlooked” caused me to write overly complex code. So I’m glad I know now (:
I need to find a way to install Windows for Workgroups 3.11 and Delphi 1 in a VMware VM somewhere to check out where it got introduced.
David Moorhouse said
Or another variant that uses an enumerated type. E.g.
type
TClinicalSection = (csClassifications, csMedications, csInbox);
…
TExtractionStatus = class
private
FDataLifeTimes: array [TClinicalSection] of integer;
function GetDataLifeTime(Index: TClinicalSection): integer;
procedure SetDataLifeTime(Index: TClinicalSection; Value: integer);
public
// access the property by index
property DataLifetime[index: TClinicalSection]: integer read GetDataLifeTime write SetDataLifeTime;
// or access each named property by name
property MedsDataLifetime: integer index csMedications read GetDataLifeTime write SetDataLifeTime;
property InboxDataLifetime: integer index csInbox read GetDataLifeTime write SetDataLifeTime;
property ClassificationsDataLifetime: integer index csClassifications read GetDataLifeTime write SetDataLifeTime;
…
jpluimers said
Those are indexed properties, and have been there and documented since like Delphi 1. The new ones are slightly different, and I could not find documentation about them. http://docwiki.embarcadero.com/RADStudio/en/Properties#Index_Specifiers
SilverWarior said
What is shown in OP code is how you can use Getter and Setter propery methods to do something more than just simply reading or writing value to internal class variables as usual.
For instance you can do something like this:
TPerson = class
private
FFirstName: String;
FLatName: String;
…
protected
GetFullName: String;
public
property FirstName: String read FFirstName write FFirstName;
property LastName: String read FLastName write FLastName;
property FullName: String read GetFullName;
end;
function TPerson.GetFullName: String;
begin
result := FFirstName+’ ‘+FLastName;
end;
You wouldn’t belive how usefull Getter and Setter peroperty methods can be.
Frankly I’m finding more nad more use for them the mroe I program.
Simelane said
I think that I have used this syntax at least since Delphi 7 when defining SOAP interface classes. I could be wrong… but I think that this has been there ever since Delphi 7.
SpeedFreak said
A similar trick can be used with records:
type
TMyRecord = record
Name: string;
Value: integer;
end;
TMyClass = class
private
FMyRecord: TMyRecord;
public
property Name: string read FMyRecord.Name;
property Value: integer read FMyRecord.Value;
end;
jpluimers said
Thanks!