Today I learned... :)

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];

Comments

  1. Igor Schevchenko I tried this in Delphi 7

    ReplyDelete
  2. Roman Yankovsky Wow! Haven't seen this too :)

    ReplyDelete
  3. Works in Delphi 6 too. I haven't got anything older here to test it.

    ReplyDelete
  4. Delphi 5 too, I just found my old notebook that still had it installed.

    ReplyDelete
  5. Well, may be somebody dont know:
    FRec: TMyRecord;
    property Prop: Integer read FRec.IntField;

    ReplyDelete
  6. Евгений Савин That one only works read only, iirc

    ReplyDelete
  7. Nothing special, as long as you go with statically allocated variables. Same as

    Private FStrings : record v0,v1: string end;
    Public property string1: string read FStrings.v1;

    ReplyDelete
  8. Daniela Osterhagen in new version. In old versions you could assign part of the records. That was handy but conceptually was a bug and later they fixed the hole.

    ReplyDelete
  9. Daniela Osterhagen  you CAN use 'write'
    type
      TMyObj = class
      private
        FRec: record Str: string end;
      public
        property StrProp: string read fRec.Str write FRec.Str;
      end;

    Arioch The 
    No, no. You say about other bug

    TMyObj2 = class
    private
      function GetRec: TRec; // TRec is record
    public
      property Prop: TRec read GetRec;
    end;

    O := TMyObj2.Create;
    O.Prop.RecordField := 10; // there was bug in old delphis, but not now

    ReplyDelete
  10. The features are interesting, but I guess, its usage increases overall code unreadability. IMO.

    Евгений Савин  Still can't assign to particular fields of record property (XE5).

    ReplyDelete
  11. > O.Prop.RecordField := 10; // there was bug in old delphis, but not now

    I'm actually not sure accepting that isn't a bug (as if the compiler accepts it, it means you can't have read-only record properties)

    ReplyDelete
  12. Igor Schevchenko 
    >>Still can't assign to particular fields of record property (XE5). 
    But in delphi 2007 or delphi 2010(i dont remember when this bug was fixed) you can.

    ReplyDelete
  13. Евгений Савин I meant:

    unit main;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;

    type
      TFoo = record
        Bar1: Integer;
        Bar2: string;
      end;

      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        FFoo: TFoo;
      public
        property Foo: TFoo read FFoo write FFoo;
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    //2006 [Pascal Error] main.pas(32): E2064 Left side cannot be assigned to
    //XE2 [DCC Error] main.pas(33): E2064 Left side cannot be assigned to
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Foo.Bar1 := 1;
    end;

    end.

    ReplyDelete
  14. I found ref to related bug http://stackoverflow.com/questions/1920140/delphi-records-in-classes (in XE4 or may be earlier fixed)  
    Well, I think it's not very important now.

    ReplyDelete

Post a Comment