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: 

Retrieve name of a variable dynamically

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.

14 REPLIES 14

matt
Active Contributor
0 Kudos

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

Sandra_Rossi
Active Contributor
0 Kudos

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.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

raymond_giuseppi
Active Contributor

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

0 Kudos

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?

pokrakam
Active Contributor

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.

0 Kudos

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.

pokrakam
Active Contributor
0 Kudos

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

horst_keller
Product and Topic Expert
Product and Topic Expert

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.

pokrakam
Active Contributor
0 Kudos

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.

matt
Active Contributor
0 Kudos

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?

0 Kudos

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

pokrakam
Active Contributor

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

horst_keller
Product and Topic Expert
Product and Topic Expert

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