www.digitalmars.com         C & C++   DMDScript  

D - Better Way of properties

reply Erick Jimenez <Erick_member pathlink.com> writes:
Hi

I see how Class work with propierties, but i think have troubles example:

class Abc
{       int myprop;
void property(int newproperty) { myprop = newproperty; } // set'er
int property() { return myprop; }       // get'er
int property(int start) { return myprop+start; }      // how work this?
}


I like delphi way (more clean):

class Abc
{       int myprop;
void prop(int newproperty) { myprop = newproperty; } // set'er
int prop() { return myprop; }       // get'er
property mypropety2: read myprop write myprop;  // direct read/write
property mypropety2: read myprop write prop;  // direct read / indirect write
property mypropety1: read prop write prop; // indeirect read/write
property mypropety2: read myprop;  // direct read only
// .....and more combinations, and you handle types, arrays, and objects too...
}
Apr 15 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Erick Jimenez" <Erick_member pathlink.com> wrote in message
news:a9erg6$urf$1 digitaldaemon.com...
 Hi

 I see how Class work with propierties, but i think have troubles example:

 class Abc
 {       int myprop;
 void property(int newproperty) { myprop = newproperty; } // set'er
 int property() { return myprop; }       // get'er
 int property(int start) { return myprop+start; }      // how work this?
 }

I guess you'll get "unresolved ambiguity" error: "property = 666" will be equal to "property(666)", which is ambiguous.
 I like delphi way (more clean):

 class Abc
 {       int myprop;
 void prop(int newproperty) { myprop = newproperty; } // set'er
 int prop() { return myprop; }       // get'er
 property mypropety2: read myprop write myprop;  // direct read/write
 property mypropety2: read myprop write prop;  // direct read / indirect

 property mypropety1: read prop write prop; // indeirect read/write
 property mypropety2: read myprop;  // direct read only
 // .....and more combinations, and you handle types, arrays, and objects

 }

I like C# way even more: class Foo { int prop1; // simple int prop2 // read-only { get { return 666; } // gettor } int prop3 { get { return m_prop3; } set { m_prop3 = prop3; } } private int m_prop3; }
Apr 15 2002
parent Erick Jimenez <Erick_member pathlink.com> writes:
In article <a9ev0o$1d27$1 digitaldaemon.com>, Pavel Minayev says...
"Erick Jimenez" <Erick_member pathlink.com> wrote in message
news:a9erg6$urf$1 digitaldaemon.com...
 Hi

 I see how Class work with propierties, but i think have troubles example:

 class Abc
 {       int myprop;
 void property(int newproperty) { myprop = newproperty; } // set'er
 int property() { return myprop; }       // get'er
 int property(int start) { return myprop+start; }      // how work this?
 }

I guess you'll get "unresolved ambiguity" error: "property = 666" will be equal to "property(666)", which is ambiguous.
 I like delphi way (more clean):

 class Abc
 {       int myprop;
 void prop(int newproperty) { myprop = newproperty; } // set'er
 int prop() { return myprop; }       // get'er
 property mypropety2: read myprop write myprop;  // direct read/write
 property mypropety2: read myprop write prop;  // direct read / indirect

 property mypropety1: read prop write prop; // indeirect read/write
 property mypropety2: read myprop;  // direct read only
 // .....and more combinations, and you handle types, arrays, and objects

 }

I like C# way even more: class Foo { int prop1; // simple int prop2 // read-only { get { return 666; } // gettor } int prop3 { get { return m_prop3; } set { m_prop3 = prop3; } } private int m_prop3; }

Yes, is more clean, but if you want multiple properties with same function, how do it?, example: class Point { private int x; int getX() { return x; } void setX(int y) { x = y; } bool evalX(int x) { return x > 2; } property X: read getX write setX; property Xgrate2: read getX write setX stored evalX; // store eveluate if x set or not property XbyStat: read getX write setX stored evalX default 15; // set X to inital value } It's not better way, but how do it?, and parses and builders is less difficult read and interpet word "property" than C# style, need more work.
Apr 15 2002