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: 

[How-to] Evaluate dynamically an ABAP Constant

laurent_chavanis
Associate
Associate
0 Kudos
1,261

Hello,

Do you know how I can dynamically/programatically retrieve the value of a defined constant in an ABAP Class having the name of the Constant.

I tried using the CLASSDESCR of the class to retrieve all attributes of type "constant", but I am not able to get the constant-value.

Thanks for your help,

Laurent

1 ACCEPTED SOLUTION

rajesh_paruchuru
Active Participant
554

Hello,

>

> Do you know how I can dynamically/programatically retrieve the value of a defined constant in an ABAP Class having the name of the Constant.

>

> I tried using the CLASSDESCR of the class to retrieve all attributes of type "constant", but I am not able to get the constant-value.

>

> Thanks for your help,

> Laurent

Seems you are trying to do something like this, not sure though

DATA:
  lv_const_name TYPE char30 VALUE 'ALIGN_AT_BOTTOM'.

FIELD-SYMBOLS:
 <fs_cons_value> TYPE ANY.

ASSIGN cl_gui_calendar=>(lv_const_name) TO <fs_cons_value>.
IF sy-subrc IS INITIAL.
  WRITE :<fs_cons_value>.
ENDIF.

You can only access constants defined as 'Public' using the above technique.

-Rajesh.

3 REPLIES 3

Former Member
0 Kudos
554

Hi

You can use the field-symbol, have you tried to give a look to the help?

Max

naimesh_patel
Active Contributor
0 Kudos
554

You can simply move the constant to get the value (or am I missing something?)


CONSTANTS: c_test TYPE char5 VALUE 'TEST'.
data: lv_temp type string.
write: / c_Test.
lv_temp = c_test.
write: / lv_temp.

Little more fancy way:


CONSTANTS: c_test TYPE char5 VALUE 'TEST'.
DATA: lo_data TYPE REF TO data.
GET REFERENCE OF c_test INTO lo_data.
FIELD-SYMBOLS: <lfs_test> TYPE ANY.

ASSIGN lo_data->* TO <lfs_test>.
WRITE: / c_test.
WRITE: / <lfs_test>.

Regards,

Naimesh Patel

rajesh_paruchuru
Active Participant
555

Hello,

>

> Do you know how I can dynamically/programatically retrieve the value of a defined constant in an ABAP Class having the name of the Constant.

>

> I tried using the CLASSDESCR of the class to retrieve all attributes of type "constant", but I am not able to get the constant-value.

>

> Thanks for your help,

> Laurent

Seems you are trying to do something like this, not sure though

DATA:
  lv_const_name TYPE char30 VALUE 'ALIGN_AT_BOTTOM'.

FIELD-SYMBOLS:
 <fs_cons_value> TYPE ANY.

ASSIGN cl_gui_calendar=>(lv_const_name) TO <fs_cons_value>.
IF sy-subrc IS INITIAL.
  WRITE :<fs_cons_value>.
ENDIF.

You can only access constants defined as 'Public' using the above technique.

-Rajesh.