07-27-2017 6:53 AM
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.
07-27-2017 7:49 AM
One question. Why? A little more explanation of why the variable name isn't known until runtime would help.
07-27-2017 8:06 AM
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.
07-27-2017 8:30 AM
07-27-2017 8:14 AM
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...
07-27-2017 8:17 AM
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?
07-27-2017 8:35 AM
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.
07-27-2017 8:45 AM
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.
07-27-2017 9:26 AM
I can't think of a scenario where this is unknown or cannot be derived. Next example? 🙂
07-27-2017 12:25 PM
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.
08-01-2017 12:25 PM
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.
07-27-2017 9:29 AM
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?
07-27-2017 10:21 AM
Yes, I'm definitely curious as to wether such a thing is possible via ABAP.
07-27-2017 11:58 AM
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>).
07-27-2017 12:28 PM
"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.