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

Retrieve name of a variable dynamically

5,292

Dear friends,

I want to store the name of a variable in a string for later use. Assume that the name of the variable is not known till runtime.

Something like:

DATA: v_myvar TYPE c. " v_myvar may not be known till runtime

DATA: varname TYPE string.

I need a way to retrieve and store 'v_myvar' in varname @ runtime.

I have tried using the cl_abap_*descr classes, but all I could get was the data type, not the name of the variable. For structures and tables it is possible to get the component list, but again how to get the name of the structure or table itself? Similarly for field-symbiols,

DATA: <fs> TYPE any.

ASSIGN v_myvar TO <fs>.

<fs> can be used to retrieve the value of v_myvar, but is it possible to retrieve the name of the var/structure/table that the field-symbol is pointing to @ runtime?

Any help would be appreciated.

With a FM like WB_TREE_SELECT you can get a list of the variable/structures defined in the program and then perform some check with some assignments, but what is your purpose, can you elaborate on it. Also how is defined the variable some obsolete global data, some attribute of a class, without clear context too many options are available...

14 REPLIES 14
Read only

matt
Active Contributor
0 Likes
4,214

One question. Why? A little more explanation of why the variable name isn't known until runtime would help.

Read only

Sandra_Rossi
Active Contributor
0 Likes
4,214

It's almost impossible to do it in ABAP, and anyway programs usually never need the name of a variable, they only need the value.

Usually, we use the statements GET REFERENCE, CREATE DATA, ASSIGN dref->* (dereferencing), or the equivalent constructor operators (REF, NEW). Please refer to the ABAP documentation.

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
4,214

Yep. In the end, you have to parse the code.

Read only

RaymondGiuseppi
Active Contributor
4,214

With a FM like WB_TREE_SELECT you can get a list of the variable/structures defined in the program and then perform some check with some assignments, but what is your purpose, can you elaborate on it. Also how is defined the variable some obsolete global data, some attribute of a class, without clear context too many options are available...

Read only

0 Likes
4,214

One possible scenario would be the use of

ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs>.

Now, I modify the value of <fs> @ runtime based on a condition and want to have a log of structure components that got modified.

I think we would have to retrieve the fieldnames for that?

Read only

pokrakam
Active Contributor
4,214

In your example

ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs>.

you can determine the component name with cl_abap_structdescr and sy-index.

Read only

0 Likes
4,214

That is as close to the solution as I have gotten. But what about singular FS or variables that are not part of any structure? Thanks for the valuable input.

Read only

pokrakam
Active Contributor
0 Likes
4,214

I can't think of a scenario where this is unknown or cannot be derived. Next example? 🙂

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
4,214

Inside a method, I want to know the name of the actual parameter passed to a formal parameter.

See CL_DEMO_INPUT and CL_DEMO_OUTPUT. It took me hours and hours to get that done.

Read only

pokrakam
Active Contributor
0 Likes
4,214

Nice one Horst! Is that why CL_DEMO_OUTPUT is clearly marked as 'not for productive use'? 😛

(sorry, it was the best response I could come up with 🙂

OK things like compiler, SCI or other language tools may need this type of functionality because the semantics are different: here the program is the subject matter (= business data). But I still still can't think of a 'regular application' scenario where good program structure can't provide the information the OP is after.

Read only

matt
Active Contributor
0 Likes
4,214

That use case is answered by Mike Pokraka. But what is the use case for "But what about singular FS or variables that are not part of any structure?"? Or is it just curiosity?

Read only

0 Likes
4,214

Yes, I'm definitely curious as to wether such a thing is possible via ABAP.

Read only

pokrakam
Active Contributor
4,214

As before, I'm curious as to why?

It's a bit like working with an importing parameter and saying "I want to know what the programmer called it in their code before calling my method". I don't care. I shouldn't care; if it were important then I have a fundamental design flaw somewhere.

Of course unnamed data objects are standard ABAP features, so I still see no scenario where knowing a name would ever be important.

assign 'Hello World' to field-symbol(<fs>).
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
4,214

"It's a bit like working with an importing parameter and saying "I want to know what the programmer called it in their code before calling my method". I don't care."

See CL_DEMO_INPUT/CL_DEMO_OUTPUT. They want to know.