Skip to Content
avatar image
-1
Former Member

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.

Add comment
10|10000 characters needed characters exceeded

  • Former Member Matthew Billingham

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

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

  • Get RSS Feed

3 Answers

  • Jul 27, 2017 at 07: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...

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 27, 2017 at 07: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.

    Add comment
    10|10000 characters needed characters exceeded

    • Nice one Horst! Is that why CL_DEMO_OUTPUT is clearly marked as 'not for productive use'? :-P

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

  • Jul 27, 2017 at 07: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.

    Add comment
    10|10000 characters needed characters exceeded