Skip to Content
-1

Retrieve name of a variable dynamically

Jul 27, 2017 at 05:53 AM

125

avatar image
Former Member

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.

10 |10000 characters needed characters left characters exceeded

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

0
Former Member
Matthew Billingham

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?

0

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
Former Member
Matthew Billingham

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

0

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

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

1
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Raymond Giuseppi
Jul 27, 2017 at 07:14 AM
1

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

Share
10 |10000 characters needed characters left characters exceeded
Mike Pokraka Jul 27, 2017 at 07:35 AM
0

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.

Show 4 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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.

0

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

0

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.

2

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.

0
Sandra Rossi Jul 27, 2017 at 07:06 AM
0

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

0