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: 

Get the Variable Name dynamically

prabhu_s2
Active Contributor

Hi

I want to get the variable name dyncamically. Here is a small example:

Data: T_HeadNr type string.

Data: Var_Name type string.

I want to move the name of the field T_HeadNr i.e. "T_HeadNr" to Var_Name and not the content of T_HeadNr.

any inputs is highly appriciated,.

thkx

Prabhu

10 REPLIES 10

Former Member
0 Kudos

Why would you want to do that.

0 Kudos

its a kinda requirement which i would want put in my code....any inputs>

I dont wanna to hardcode

Message was edited by:

Prabhu S

0 Kudos

If you define the fields within a stucture you can use the code

data: i_components type standard table of rstrucinfo.

data w_repid type syrepid.

w_repid = sy-repid.

call function 'GET_COMPONENT_LIST'

exporting

program = w_repid

fieldname = 'ST_RECORDS'

tables

components = i_components.

0 Kudos

i dont want this 'ST_RECORDS' to be hardocded. this is my requirement. i need to pass the name dynamically to this f/n module !!!

0 Kudos

tell me if this helps

data: begin of myitab occurs 0,
  number type i,
  name(30),
 end of myitab.


data: INT_TABNAME(40).
INT_TABNAME = 'MYITAB'.

* Insert internal table fieldname into itab
DATA : IRSTRUCINFO LIKE RSTRUCINFO OCCURS 0 WITH HEADER LINE.
CALL FUNCTION 'GET_COMPONENT_LIST'
  EXPORTING
    PROGRAM    = sy-repid
    FIELDNAME  = INT_TABNAME
  TABLES
    COMPONENTS = IRSTRUCINFO.

loop at IRSTRUCINFO.
  write:/ IRSTRUCINFO-COMPNAME.
endloop.

write:/ 'end'.

0 Kudos

thkx kris for ur reply. but again i dont want any hardcoding such as INT_TABNAME = 'MYITAB'. is it possible to get the name of 'MYITAB' into INT_TABNAME??????

0 Kudos

MYITAB is the name of an internal table containing the fields

when you ask the program to fetch the fieldnames, you need to tell it from where to fetch it

0 Kudos

there are some classes that gives u the type references of the internal table. in this case is there any similar case where we can get the name of the variable itself?

0 Kudos

For classes, do you not need to specify the internal table name.

You will have to hardcode something.

Former Member
0 Kudos

You will have to hardcode something. The program cannot assume what structure/field you require.

What is reason you do not want to hardcode the structure.

Message was edited by:

Martin Shinks