The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,858 other subscribers

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….

10 Responses to “Today I learned… :) I didn’t know about this syntax for properties… (via: Roman Yankovsky – Google+)”

  1. I suppose any static property field can be accessed directly without setter and getter methods?

  2. Thaddy's avatar

    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's avatar

      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.

  3. David Moorhouse's avatar

    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;

  4. SilverWarior's avatar

    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.

  5. Simelane's avatar

    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.

  6. SpeedFreak's avatar

    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;

Leave a reply to jpluimers Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.