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];
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];
What the version of Delphi ?
ReplyDeleteIgor Schevchenko I tried this in Delphi 7
ReplyDeleteVery interesting...
ReplyDeleteRoman Yankovsky Wow! Haven't seen this too :)
ReplyDeleteWorks in Delphi 6 too. I haven't got anything older here to test it.
ReplyDeleteDelphi 5 too, I just found my old notebook that still had it installed.
ReplyDeleteWell, may be somebody dont know:
ReplyDeleteFRec: TMyRecord;
property Prop: Integer read FRec.IntField;
Евгений Савин That one only works read only, iirc
ReplyDeleteNothing special, as long as you go with statically allocated variables. Same as
ReplyDeletePrivate FStrings : record v0,v1: string end;
Public property string1: string read FStrings.v1;
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.
ReplyDeleteDaniela Osterhagen you CAN use 'write'
ReplyDeletetype
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
The features are interesting, but I guess, its usage increases overall code unreadability. IMO.
ReplyDeleteЕвгений Савин Still can't assign to particular fields of record property (XE5).
> O.Prop.RecordField := 10; // there was bug in old delphis, but not now
ReplyDeleteI'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)
Igor Schevchenko
ReplyDelete>>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.
Евгений Савин I meant:
ReplyDeleteunit 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.
I found ref to related bug http://stackoverflow.com/questions/1920140/delphi-records-in-classes (in XE4 or may be earlier fixed)
ReplyDeleteWell, I think it's not very important now.