Page 1 of 1

Class DATA getSet

Posted: Thu Sep 04, 2014 6:02 am
by karweru
Hi Friends,

I want to illustrate my question with the code below;

Code: Select all


CREATE CLASS student
DATA name
DATA age
EXPORT:
METHOD name(cName) INLINE ::getSet("name",cName)
METHOD age(nAge) INLINE ::getSet("age",nAge)
METHOD getSet(xVar,xVal)
ENDCLASS

METHOD init()
::name:=""
::age=:=0
RETURN self

METHOD getSet(xVar,xName)
nPars:=pCount()
do case 

case (xVar:=upper(xVar))=="NAME"
 if nPars==2
::name:=xVal
endif
return ::name

case xvar=="AGE"
if nPars==2
::age:=xVal
endif
return ::age
endcase
return Nil
My question is about the getset() method manner of coding. Is there a function that can be used to get/set DATA in a class. I stumbled on classSel() which can list all the DATA in the class but is there a function(s) that can be used so that the getSet() method above can be coded as below;

getDataFunction(<xVar>)

setDataFunction(<xVar>,<xVal>)

Thank you in advance.

Re: Class DATA getSet

Posted: Wed Sep 24, 2014 11:11 am
by esgici
karweru wrote:...
My question is about the getset() method manner of coding. Is there a function that can be used to get/set DATA in a class.
Hi Gilbert

Frankly, I'm not an OOP expert.

Recently past a thread in Harbour users forum, including a document on Harbour OOP.

I hope it may be useful.

Happy HMG'ing :D

Re: Class DATA getSet

Posted: Wed Sep 24, 2014 12:35 pm
by karweru
Hi Esgici,

Thank you very much for your interest in this subject...I was able to resolve the question and consequently used this style to re-write HMGobjects, very compressed/shortened verson, for use in my projects.
The solution was using macros as below, (using the same code above for illustration);

Code: Select all

CREATE CLASS student
DATA name
DATA age
EXPORT:
METHOD name(cName) INLINE ::getSet("name",cName)
METHOD age(nAge) INLINE ::getSet("age",nAge)
METHOD getSet(xVar,xVal)
ENDCLASS

METHOD init()
::name:=""
::age=:=0
RETURN self

METHOD getSet(xVar,xName)
local get_Var,set_Var

get_var:="qself():_"+xVar
set_var:="qself():"+xVar

if valType(xVal)!="U"
&set_Var:=xVal
endif
return &get_Var

Thank you again Esgici.

Re: Class DATA getSet

Posted: Wed Sep 24, 2014 12:50 pm
by esgici
Hi Gilbert

I was should interested; because your question left 20 days unanswered;

and I know well what is being left unanswered, waiting unresponsive :(

I'm glad that you have found yourself an answer to your question.

And thanks to share your code with us :)

Happy HMG'ing :D

Re: Class DATA getSet

Posted: Wed Sep 24, 2014 3:25 pm
by dhaine_adp
Hi Gilbert,

Here's a shortened version of your solution without the local variables on method getSet():

Code: Select all

CREATE CLASS student
   DATA name INIT NIL HIDDEN
   DATA age  INIT  0 HIDDEN

   EXPORT:
   METHOD New() CONSTRUCTOR
   METHOD name(cName) INLINE ::getSet("name",cName)
   METHOD age(nAge) INLINE ::getSet("age",nAge)
   METHOD getSet(xVar,xVal)
ENDCLASS

METHOD New()
   RETURN self

METHOD getSet( cPropName, xVal )

   IF PCOUNT() == 1
      return ::&( cPropName )
   ELSE
      ::&( cPropName ) := xVal
   ENDIF
   RETURN Self

Re: Class DATA getSet

Posted: Wed Sep 24, 2014 4:20 pm
by Rathinagiri
Interesting!