Application Development and Automation 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: 
Read only

Dynamic access to attributes

Former Member
0 Likes
4,188

Hi all

I have a class with many public static attributes. To provide a generic solution I want to access the value of such an attribute dynamically at run-time. Unfortunately am I not able to do it. Can you help me with this issue?

The following code fragment for instance is not allowed. In this case I receive the syntax error that 'Dynamic attribute declarations are not supported in the current statement'.


DATA: 
lv_txt         TYPE string,
lv_attribute   TYPE string.

lv_attribute = 'TEST'.
lv_txt = Z_TEST=>(lv_attribute).

Any hint is welcome...

Thanks, Mathias

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
1,975

You have to access it using dynamic assing


FIELD-SYMBOLS <attr> TYPE string.

ASSIGN cl_abap_char_utilities=>('HORIZONTAL_TAB') TO <attr>.

Regards

Marcin

For reference [Accessing Class Components |http://help.sap.com/abapdocu_70/en/ABENCLASS_COMPONENTS_ADDRESSING.htm]

Edited by: Marcin Pciak on Jun 2, 2010 10:16 AM

Hi all

I have a class with many public static attributes. To provide a generic solution I want to access the value of such an attribute dynamically at run-time. Unfortunately am I not able to do it. Can you help me with this issue?

The following code fragment for instance is not allowed. In this case I receive the syntax error that 'Dynamic attribute declarations are not supported in the current statement'.


DATA: 
lv_txt         TYPE string,
lv_attribute   TYPE string.

lv_attribute = 'TEST'.
lv_txt = Z_TEST=>(lv_attribute).

Any hint is welcome...

Thanks, Mathias

4 REPLIES 4
Read only

Former Member
0 Likes
1,975

hi,

you could do a little workaround by calling a get-method dynamicly, which will return the attribute you would like to read:

CALL METHOD Z_TEST=>(ld_methodname)

greetings,

dsp

Read only

Former Member
0 Likes
1,975

Hi dsp

I don't think that this will do the trick. If I just call a get method and provide the name of the attribute as import parameter, I get method must now determine the attribute dynamically. I want to avoid in any case any SWITCH or IF cases, I want to access attribute values dynamically.

Regards, Mathias

Read only

0 Likes
1,975

hi,

you could concatenate the methodname, but you would still need to implement a real method which will returnjust the specific attribute:

concatenate 'get_' ld_attribute into ld_methodname

Read only

MarcinPciak
Active Contributor
0 Likes
1,976

You have to access it using dynamic assing


FIELD-SYMBOLS <attr> TYPE string.

ASSIGN cl_abap_char_utilities=>('HORIZONTAL_TAB') TO <attr>.

Regards

Marcin

For reference [Accessing Class Components |http://help.sap.com/abapdocu_70/en/ABENCLASS_COMPONENTS_ADDRESSING.htm]

Edited by: Marcin Pciak on Jun 2, 2010 10:16 AM