10-01-2008 9:14 AM
Hi experts,
Can anyone help me in next situation.
I need access class static attribute by class name and attribute name. I have two initialized variables
DATA: class_name(30) TYPE c,
attr_name(30) TYPE c.
In another words, I need to do attribute initializing like
ZCL_IM_BM_VPA10_IMPL=>P_VAR_ZPLV_CLM = loc_var_range-low.
but using char variables insead of reference to class.
Thanks in advance,
Max.
10-01-2008 9:23 AM
Hi
U can use the field-symbols:
CLASS LCL_MY_CLASS DEFINITION.
PUBLIC SECTION.
CLASS-DATA: VAR1 TYPE C,
VAR2 TYPE C.
ENDCLASS.
CLASS LCL_MY_CLASS IMPLEMENTATION.
ENDCLASS.
DATA: CLASS_NAME(30) TYPE C,
CLASS_VAR(30) TYPE C,
CLASS_REF(100) TYPE C.
FIELD-SYMBOLS: <VAR> TYPE ANY.
CLASS_NAME = 'LCL_MY_CLASS'.
CLASS_VAR = 'VAR1'.
CONCATENATE CLASS_NAME '=>' CLASS_VAR INTO CLASS_REF.
ASSIGN (CLASS_REF) TO <VAR>.
IF SY-SUBRC = 0.
<VAR> = 'A'.
ENDIF.
WRITE: LCL_MY_CLASS=>VAR1.
Max
10-01-2008 9:17 AM
You want dynamic access to a static attribute? If these are your own Z classes, then rather than access the attributes directly, write getter and setter methods.
Then you can use (as it is in the ABAP help on dynamic call methods ).
TRY.
CALL METHOD (class)=>(meth)
PARAMETER-TABLE
ptab
EXCEPTION-TABLE
etab.
CASE sy-subrc.
WHEN 1.
...
...
ENDCASE.
CATCH cx_sy_dyn_call_error INTO exc_ref.
exc_text = exc_ref->get_text( ).
MESSAGE exc_text TYPE 'I'.
ENDTRY.
btw - next time, post in the ABAP objects forum.
matt
10-01-2008 9:25 AM
Thank you for your answer.
But is it possible access class attributes or methods in this way if i know only name of class. This name was generated automaticaly and contained in character variable.
It is the same as use next code
CALL METHOD 'AN_CLASS'=>''AN_METHOD'.....
10-01-2008 9:23 AM
Hi
U can use the field-symbols:
CLASS LCL_MY_CLASS DEFINITION.
PUBLIC SECTION.
CLASS-DATA: VAR1 TYPE C,
VAR2 TYPE C.
ENDCLASS.
CLASS LCL_MY_CLASS IMPLEMENTATION.
ENDCLASS.
DATA: CLASS_NAME(30) TYPE C,
CLASS_VAR(30) TYPE C,
CLASS_REF(100) TYPE C.
FIELD-SYMBOLS: <VAR> TYPE ANY.
CLASS_NAME = 'LCL_MY_CLASS'.
CLASS_VAR = 'VAR1'.
CONCATENATE CLASS_NAME '=>' CLASS_VAR INTO CLASS_REF.
ASSIGN (CLASS_REF) TO <VAR>.
IF SY-SUBRC = 0.
<VAR> = 'A'.
ENDIF.
WRITE: LCL_MY_CLASS=>VAR1.
Max
10-01-2008 10:24 AM