IceCold
IceCold

Reputation: 21154

Is it possible to have two properties with the same name?

Is it possible to have two properties with the same name?

property  Cell [Cl, Rw: Integer]: string   read getCell  write setCell;
property  Cell [ColName: string; Rw: Integer]: string read getCellByCol write setCellByCol;

Well, I tried it and the compiler won't let me do it, but maybe there is a trick...?

Upvotes: 16

Views: 3832

Answers (1)

HeartWare
HeartWare

Reputation: 8243

No - but then again: Yes... Sort of...

function    getP1(Cl,Rw : integer) : string;
procedure   setP1(C1,Rw : integer ; const s : string);
function    getP2(const Cl : string ; Rw : integer) : string;
procedure   setP2(const C1 : string ; Rw : integer ; const s : string);
property    P1[Cl,Rw : integer] : string read getP1 write setP1; default;
property    P1[const Cl : string ; Rw : integer] : string read getP2 write setP2; default;

The trick is to name the property the same, and to mark both with "default" clause. Then you can access the same property name with various parameters:

P1['k',1]:=P1[2,1];
P1[2,1]:=P1['k',1];

compiles fine.Don't know if this is offcially supported or if there's some other problems with it, but it compiles fine and calls the correct getter/setter (tested in Delphi 2010).

This of course only works if you don't already use a default property for your class, as the only way I have been able to make it work is via the default clause.

Upvotes: 31

Related Questions