‎2010 Apr 16 7:07 PM
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
‎2010 Apr 16 7:26 PM
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
‎2010 Apr 16 7:25 PM
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
‎2010 Apr 16 7:26 PM
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