cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get the name of a variable into a variable?

Former Member
0 Kudos

Hi,

I declare a variable and later I want to use not only the value but also the (declared) name in the ABAP program (dynamically).

Assign? Or with a class? I searched the CL_ABAP... classes but without success.

Example:

Data: myname(30) type c.

data: variablenames(100) type c.

variablenames = ....?

How can I get "MYNAME" dynamically as value into variablenames?

Werner

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Ok, helpful so far but it does not solve my problem. It could be, that there is no solution.

I want to create a name/value pair (in the context of XML). The name should be the name of the variable, the value is the value of it.

I want to write a method with input paramter the variable and output parameters the name and value of the entered variable.

->myname (with value e.g. Werner)

<-name (with value MYNAME) ...this is variablenames

<-value (with value WERNER) ...this is there anyway

The tricky thing is, that the declaration of myname is a line in the program anywhere (or in the calling program). So I think it is not possible to dynamically get the name of a variable at runtime.

My workaround is to write a function like this:

->myname (with value e.g. Werner)

->nameofvariable (with value 'MYNAME')

<-name (with value MYNAME) ...this is variablenames

<-value (with value WERNER) ...this is there anyway

Thank you. Any thoughts welcome.

Werner

athavanraja
Active Contributor
0 Kudos

have a look at the FM

WB_TREE_RETURN_OBJECT_LIST

if you pass PG_<program name> to the tree name parameter of this function the NODETAB (tables parameter) will have all the fields (variables) (in nodetab where type eq 'OPD') declared in the mentioned program.

then using this fieldname and field symbol you can get the value assigned to it and create the name / value pair.

any how the simplest soultion would be that if you are using WAS 6.1 >

you could simply use CALL TRANSFORMATION to generate the xml for you.

sample code using call transformation.

data: myname(6) ,
      xml_out type string .


myname = 'RAJA' .

CALL TRANSFORMATION (`ID`)
          SOURCE myname   = myname
          RESULT XML xml_out.

check out the generated xml in <b>xml_out</b> .

Regards

Raja

Former Member
0 Kudos

Hi

this is a simple code to create a variable:

PARAMETERS: p_field(100),

p_value(200).

DATA code(172) OCCURS 1.

DATA: code1(200),

length TYPE i.

DATA: L_FORM(20) VALUE 'MY_NAME',

l_name LIKE sy-repid VALUE sy-repid.

CODE1 = 'REPORT GENENERATED_SUBROUTINE_POOL.'.

APPEND code1 TO code.

CODE1 = 'FORM MY_NAME.'.

APPEND code1 TO code.

length = STRLEN( p_field ).

code1 = 'DATA &(20).'.

REPLACE '&' WITH p_field(length) INTO code1.

CONDENSE code1.

APPEND code1 TO code.

CONCATENATE p_field(length) '=' '''&''' INTO code1

SEPARATED BY space.

length = STRLEN( p_value ).

REPLACE '&' WITH p_value(length) INTO code1.

CONCATENATE code1 '.' INTO code1.

APPEND code1 TO code.

code1 = 'WRITE &.'.

length = STRLEN( p_field ).

REPLACE '&' WITH p_field(length) INTO code1.

CONDENSE code1.

APPEND code1 TO code.

CODE1 = 'ENDFORM.'.

APPEND code1 TO code.

CATCH SYSTEM-EXCEPTIONS generate_subpool_dir_full = 9.

GENERATE SUBROUTINE POOL code NAME l_name.

ENDCATCH.

PERFORM (L_FORM) IN PROGRAM (L_NAME).

Max

Answers (3)

Answers (3)

Clemenss
Active Contributor
0 Kudos

Hi,

I'm not sure it will be possible to retrieve the (source) variable name easily, because as a matter of fact the name used in source code is of no importance in the generated program. Actually I don't know how they do it in debugger, but this is kernel.

But, if you can use different types (rollnames, data elememts) fpr your vaiables, here is a way to find out the data type including name of the dictionary type:

FORM varname .

TYPE-POOLS:

sydes.

DATA:

l_varname TYPE fieldname,

ls_sydes_desc TYPE sydes_desc.

FIELD-SYMBOLS:

<names> TYPE LINE OF sydes_desc-names.

DESCRIBE FIELD l_varname INTO ls_sydes_desc.

READ TABLE ls_sydes_desc-names

ASSIGNING <names> INDEX 2.

WRITE: / <names>-name.

ENDFORM. " varname

The program will print out FIELDNAME.

I used this method for the automatic creation of field catalogs for ALV - dynamically just for any internal table and it works fine.

Just a note: the sydes_desc object consists of 2 internal tables, 'types' and 'names'. 'types' gives the technical characteristics and 'names' the name. When describing a simple variable, names line 1 is the program name and line 2 the field name.

Hope you can use it.

regards,

C.

Former Member
0 Kudos

Try This. You just have to include in () . This will point to the varaiable name stored in it .

Data: myname(30) type c.

data: variablenames(100) type c.

field-symbols <F1> type any.

variablenames = 'myname'.

assign (variablenames) to <F1> .

myname = 'WERNER'.

write 😕 <F1>.

Cheers.

Former Member
0 Kudos

Hi Werner - could you please describe your requirements more fully. You can do something like the following, but I'm not sure it's what you want:


DATA: var1,
      var2,
      var3,
      var4,
      var5,
      var6,
      comp(30).
var3 = 'X'.
CASE 'X'.
  WHEN var1.
    MOVE 'var1' TO comp.
  WHEN var2.
    MOVE 'var2' TO comp.
  WHEN var3.
    MOVE 'var3' TO comp.
  WHEN var4.
    MOVE 'var4' TO comp.
  WHEN var5.
    MOVE 'var5' TO comp.
  WHEN var6.
    MOVE 'var6' TO comp.
ENDCASE.

Rob