Application Development and Automation 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: 
Read only

Dynamic component reference

Former Member
0 Likes
905

In the following excerpt of code, I have a structure definition called ty_costs. I am being able to obtain the names of the components of this structure, via use of types abap_compdescr_tab and cl_abap_structdescr. Why would I need to know the names of the components at runtime? Because there's a table from ty_costs type, in which I have, for every row, to find the sum of a certain range of the wkg*** variables - based on two parameters imported to this function module I'm building. In other words, For each row of the table, I have to sum the contents of the variables wkg***, but not all of them - only those that stand between the starting and ending limits that are going to be passed as parameters - for instance, "01" and "05", would make me have to sum only wkg001 to wkg005.

I thought I would be able to do something like work_area_for_table_of_type_ty_costs-<fs> , where <fs> would point to the name of the each of the wkg***s variables I have to consider. But the compiler doesn't let me do that, it seems that the compiler doesn't allow runtime component assignments.

Does anyone see any other way I can do it ? How would be that ?

Thanks in advance,

Avraham

TYPES: BEGIN OF ty_costs,
        lednr   TYPE lednr,
        objnr   TYPE j_objnr,
        gjahr   TYPE gjahr,
        wrttp   TYPE co_wrttp,
        versn   TYPE versn,
        kstar   TYPE kstar,
        hrkft   TYPE co_subkey,
        vrgng   TYPE co_vorgang,
        beknz   TYPE beknz,
        twaer   TYPE twaer,
        wkg001  TYPE wkgxxx,
        wkg002  TYPE wkgxxx,
        wkg003  TYPE wkgxxx,
        wkg004  TYPE wkgxxx,
        wkg005  TYPE wkgxxx,
        wkg006  TYPE wkgxxx,
        wkg007  TYPE wkgxxx,
        wkg008  TYPE wkgxxx,
        wkg009  TYPE wkgxxx,
        wkg010  TYPE wkgxxx,
        wkg011  TYPE wkgxxx,
        wkg012  TYPE wkgxxx,
        wkg013  TYPE wkgxxx,
        wkg014  TYPE wkgxxx,
        wkg015  TYPE wkgxxx,
        wkg016  TYPE wkgxxx,
       END OF ty_costs.

  DATA:
        ls_components             TYPE abap_compdescr_tab,
        lo_strucdescr                TYPE REF TO cl_abap_structdescr,

  FIELD-SYMBOLS: <fs> TYPE ty_costs,

  lo_strucdescr ?= cl_abap_typedescr=>describe_by_data( <fs> ).
  ls_components = lo_strucdescr->components.

1 ACCEPTED SOLUTION
Read only

former_member194669
Active Contributor
0 Likes
791

Try something like this way


  v_start_no = 02.      " Say for example 02
  v_end_no = 06.        " Say for example 06
  
do.
  concatenate 'TY_COSTS-WKG0' v_start_no into v_field
  condense v_field no-gaps.
  assign ( v_field ) to <fs>.
  v_total = v_total + <fs>. 
  if v_end_no = v_start_no.
     exit.
  else.
     v_start_no = v_satrt_no + 1.
  endit.
 
enddo.

Here v_total contains the total of WKG002 to WKG006 values

a®

5 REPLIES 5
Read only

former_member194669
Active Contributor
0 Likes
792

Try something like this way


  v_start_no = 02.      " Say for example 02
  v_end_no = 06.        " Say for example 06
  
do.
  concatenate 'TY_COSTS-WKG0' v_start_no into v_field
  condense v_field no-gaps.
  assign ( v_field ) to <fs>.
  v_total = v_total + <fs>. 
  if v_end_no = v_start_no.
     exit.
  else.
     v_start_no = v_satrt_no + 1.
  endit.
 
enddo.

Here v_total contains the total of WKG002 to WKG006 values

a®

Read only

0 Likes
791

Thank you very much, guys.

The whole thing was about the ( ) surrounding the field symbol. I wasn't using it in my program, so it wasn't actually working like language C's *pointer, and that was all I wanted

I didn't know about this thing of having to enclose the variable to be assigned to a fs ( ) - only then a fs will understand it must point to the contents of the memory address being passed for assignment, instead of the contents of the variable itself. Right ?

I'm glad I learned this.

Well, since you all guys gave me more or less the same solutions (basically the only difference was some of you used ASSIGN COMPONENT) - but my program itself did look pretty much like the snippets you posted, I'm gonna try to distribute the points according to the precedence of the answers.

Thanks again !

Avraham

Read only

Former Member
0 Likes
791

Hi

U need to use the field-symbols in order to do it.

TYPES: BEGIN OF TY_RANGE,
                   SIGN(1),
                   OPTION(2),
                   LOW        TYPE abap_compname
                   HIGH       TYPE abap_compname
               END OF TY_RANGE.

DATA: T_RANGE TYPE TABLE OF TY_RANGE.
DATA: W_RANGE TYPE TY_RANGE.

FIELD-SYMBOLS: <FS> TYPE ANY.


W_RANGE(3) = 'IBT'.
W_RANGE-LOW = <From field name>.
W_RANGE-HIGH = <to field name>.
APPEND W_RANGE TO T_RANGE.

LOOP AT ls_components where name in T_RANGE.
   ASSIGN COMPONENT ls_components-NAME of structure <STRUCTURE> to <FS>.
  IF sy-subrc = 0.
     total = total + <fs>.
  endif.
ENDLOOP. 

Max

Read only

naimesh_patel
Active Contributor
0 Likes
791

Once, you have the name of the required fields, you can use the ASSIGN COMPONENT comp OF STRUCTURE stru TO <F_FIELD>.

Like:


L_NUM = 5.
DO 5 TIMES.
L_NUM = L_NUM + 1.
CONCATENATE 'WKG' L_NUM INTO L_COMPONENT.
ASSIGN COMPONENT L_COMPONENT OF STRUCUTRE LA_COSTS TO <F_AMT>.
....
ENDDO.

Regards,

Naimesh Patel

Read only

Former Member
0 Likes
791

Pl. check this code..may be it will help u.

&----


*& Report Z_DEMO_JG

*&

&----


*&

*& Download graphics from BDS

&----


REPORT z_demo_jg.

types: begin of ty_fld,

fld1 type i,

fld2 type i,

fld3 type i,

fld4 type i,

fld5 type i,

fld6 type i,

fld7 type i,

fld8 type i,

fld9 type i,

end of ty_fld.

data: l_total type i.

data: i_fld type standard table of ty_fld.

data: w_fld type ty_fld.

do 10 times.

w_fld-fld1 =

w_fld-fld2 =

w_fld-fld3 =

w_fld-fld4 =

w_fld-fld5 =

w_fld-fld6 =

w_fld-fld7 =

w_fld-fld8 =

w_fld-fld9 = sy-index.

append w_fld to i_fld.

enddo.

loop at i_fld into w_fld.

perform calc using sy-tabix 9.

write : / l_total.

clear l_total.

endloop.

&----


*& Form calc

&----


  • text

----


  • -->P_MIN text

  • -->P_MAX text

  • -->P_W_FLD text

----


form calc using p_min type i

p_max type i.

data: l_char(15) type c.

data: l_no(2) type c.

field-symbols: <fs> type i.

while p_min <= p_max.

l_no = p_min.

concatenate 'W_FLD-FLD' l_no into l_char.

condense l_char no-gaps.

assign (l_char) to <fs>.

check sy-subrc = 0.

l_total = l_total + <fs>.

p_min = p_min + 1.

endwhile. " calc

endform.

Regards,

JOy.