Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Access class attribute by class name

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

4 REPLIES 4

matt
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

0 Kudos

Thank you Max. Your solution is helped to me.