Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 908
Server Overflow Avatar asked Sep 15 '15 10:09

Server Overflow


People also ask

Can two properties have the same name?

Accepted Answer. An anonymous object can't have two properties with the same name. Change your anonymous projection, give a different name to the second Title property.

Can we have two properties with the same name inside an object?

You cannot. Property keys are unique.


1 Answers

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.

like image 103
HeartWare Avatar answered Oct 01 '22 19:10

HeartWare