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

declaring type definition dynamically

Former Member
0 Likes
2,100

hi guys,

i have a strange issue. i need to define a varaible that refers to a below type definition.

types : begin of t_str,

var1 type string,

vcar2 type string,

.

.

.

.

end of t_str,

data: variable type t_str.

the problem is i do not know how many var's will be there until run time. so my type definition needs to be dynamic. is there any way to do it.

thanks

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
928

You can create a dynamic structure like this. <fs_structure> will be the structure with 20 string fields.

TYPE-POOLS: abap.

DATA:
  lr_structdescr    TYPE REF TO cl_abap_structdescr,
  lr_datadescr      TYPE REF TO cl_abap_datadescr,
  lt_components     TYPE abap_component_tab,
  ls_component      TYPE LINE OF abap_component_tab,
  lr_struct         TYPE REF TO data.

DATA: lv_index_num(3) TYPE n.
DATA: lv_index_char(3) TYPE c.

FIELD-SYMBOLS: <fs_structure> TYPE ANY.

START-OF-SELECTION.

  DO 20 TIMES.
    lv_index_num = sy-index.
    lv_index_char = lv_index_num.
    CONCATENATE 'value' lv_index_char INTO ls_component-name.
    ls_component-type ?= cl_abap_elemdescr=>get_string( ).
    INSERT ls_component INTO TABLE lt_components.
  ENDDO.

* get structure descriptor -> lr_STRUCTDESCR
  lr_structdescr ?= cl_abap_structdescr=>create( lt_components ).

* create a structure
  CREATE DATA lr_struct TYPE HANDLE lr_structdescr.
  ASSIGN lr_struct->* TO <fs_structure>.

Regards,.

Rich Heilman

2 REPLIES 2
Read only

naimesh_patel
Active Contributor
0 Likes
928

You need to use RTTS to generate the Dynamic Type for you. Check the Example in this post: http://help-abap.blogspot.com/2008/09/dynamic-internal-table-creation.html

Once you create the type in the given example, you need to assign that type to the Field Symbol to work with it.


* 3. Create a New Type
  lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).

* 5. data to handle the new table type
  CREATE DATA lo_data TYPE HANDLE lo_new_type.
*
* 6. New internal table in the fieldsymbols
  field-symbols: <f_structure> type any.
  ASSIGN lo_data->* TO <f_structure>

* Access fields within it
  field-symbols: <fs_field> type any.
  assign component 'KSTAR' of structure <lfs_strucutre> to <lfs_field>.

Regards,

Naimesh Patel

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
929

You can create a dynamic structure like this. <fs_structure> will be the structure with 20 string fields.

TYPE-POOLS: abap.

DATA:
  lr_structdescr    TYPE REF TO cl_abap_structdescr,
  lr_datadescr      TYPE REF TO cl_abap_datadescr,
  lt_components     TYPE abap_component_tab,
  ls_component      TYPE LINE OF abap_component_tab,
  lr_struct         TYPE REF TO data.

DATA: lv_index_num(3) TYPE n.
DATA: lv_index_char(3) TYPE c.

FIELD-SYMBOLS: <fs_structure> TYPE ANY.

START-OF-SELECTION.

  DO 20 TIMES.
    lv_index_num = sy-index.
    lv_index_char = lv_index_num.
    CONCATENATE 'value' lv_index_char INTO ls_component-name.
    ls_component-type ?= cl_abap_elemdescr=>get_string( ).
    INSERT ls_component INTO TABLE lt_components.
  ENDDO.

* get structure descriptor -> lr_STRUCTDESCR
  lr_structdescr ?= cl_abap_structdescr=>create( lt_components ).

* create a structure
  CREATE DATA lr_struct TYPE HANDLE lr_structdescr.
  ASSIGN lr_struct->* TO <fs_structure>.

Regards,.

Rich Heilman