‎2008 Oct 28 2:16 PM
I want to create a variable with a pre-defined TYPE, but the latter will only be known at runtime.
For instance:
Consider that I my DEFINITIONS1 include file is:
TYPES: BEGIN OF type1,
field1 TYPE i,
field2 TYPE i,
field3 TYPE i,
END OF type1,
BEGIN OF cust_type_A,
INCLUDE TYPE type1.
TYPES: erdat TYPE vbak-erdat,
END OF cust_type_A,
BEGIN OF cust_type_B,
INCLUDE TYPE type1.
TYPES: fkdat TYPE vbrk-fkdat,
END OF cust_type_B.-
Than I have the following code (assume "letter" is a character parameter I receive, either 'A' or 'B'):
INCLUDE definitions1.
DATA: z TYPE REF to data.
FIELD-SYMBOLS: <fs> TYPE ANY.
DATA str type string.
CONCATENATE 'MY_CUST_TYPE_' letter INTO str.
CREATE DATA z TYPE (str).
* ASSIGN z->* TO <fs>.Why is that that the CREATE DATA z TYPE (str) statement fails ?
How, then, I create a variable/field symbol from a specific type, which will only be known at runtime ?
Thanks
Avraham
‎2008 Oct 28 2:22 PM
Hi,
It is failing because your declarations are cust_type_A and you are trying to type it to MY_CUST_TYPE_A.
Remove the MY_ and it will work.
e.g.
types: begin of type1,
field1 type i,
field2 type i,
field3 type i,
end of type1,
begin of cust_type_a,
include type type1.
types: erdat type vbak-erdat,
end of cust_type_a,
begin of cust_type_b,
include type type1.
types: fkdat type vbrk-fkdat,
end of cust_type_b.
data: z type ref to data.
data str type string.
field-symbols: <fs> type any.
parameters: letter type char1 default 'A'.
start-of-selection.
concatenate 'CUST_TYPE_' letter into str.
create data z type (str).Darren
‎2008 Oct 28 2:22 PM
Hi,
It is failing because your declarations are cust_type_A and you are trying to type it to MY_CUST_TYPE_A.
Remove the MY_ and it will work.
e.g.
types: begin of type1,
field1 type i,
field2 type i,
field3 type i,
end of type1,
begin of cust_type_a,
include type type1.
types: erdat type vbak-erdat,
end of cust_type_a,
begin of cust_type_b,
include type type1.
types: fkdat type vbrk-fkdat,
end of cust_type_b.
data: z type ref to data.
data str type string.
field-symbols: <fs> type any.
parameters: letter type char1 default 'A'.
start-of-selection.
concatenate 'CUST_TYPE_' letter into str.
create data z type (str).Darren
‎2008 Oct 28 2:28 PM
‎2008 Oct 28 2:45 PM
Check this code to create a dynamic type at run time using the RTTS - Run Time Type Services.
DATA: lo_struct TYPE REF TO cl_abap_structdescr,
lo_element TYPE REF TO cl_abap_elemdescr,
lo_new_type TYPE REF TO cl_abap_structdescr,
lo_data TYPE REF TO data,
lt_comp TYPE cl_abap_structdescr=>component_table,
lt_tot_comp TYPE cl_abap_structdescr=>component_table,
la_comp LIKE LINE OF lt_comp.
DATA: lc_num TYPE char10.
* field symbols to access the dynamic table
FIELD-SYMBOLS: <f_line> TYPE ANY,
<f_field> TYPE ANY.
PARAMETERS: p_a RADIOBUTTON GROUP rd1,
p_b RADIOBUTTON GROUP rd1.
START-OF-SELECTION.
DO 3 TIMES.
lc_num = sy-index.
CONDENSE lc_num.
* Element Description
lo_element ?= cl_abap_elemdescr=>describe_by_name( 'INT4' ).
*
* Field name
CONCATENATE 'FIELD' lc_num INTO la_comp-name.
*
* Field type
la_comp-type = cl_abap_elemdescr=>get_p(
p_length = lo_element->length
p_decimals = lo_element->decimals ).
*
* Filling the component table
APPEND la_comp TO lt_tot_comp.
CLEAR: la_comp.
ENDDO.
IF p_a = 'X'.
* Element Description
lo_element ?= cl_abap_elemdescr=>describe_by_name( 'ERDAT' ).
*
* Field name
la_comp-name = 'ERDAT'.
*
* Field type
la_comp-type = cl_abap_elemdescr=>get_p(
p_length = lo_element->length
p_decimals = lo_element->decimals ).
*
* Filling the component table
APPEND la_comp TO lt_tot_comp.
CLEAR: la_comp.
ELSEIF p_b = 'X'.
* Element Description
lo_element ?= cl_abap_elemdescr=>describe_by_name( 'FKDAT' ).
*
* Field name
la_comp-name = 'FKDAT'.
*
* Field type
la_comp-type = cl_abap_elemdescr=>get_p(
p_length = lo_element->length
p_decimals = lo_element->decimals ).
*
* Filling the component table
APPEND la_comp TO lt_tot_comp.
CLEAR: la_comp.
ENDIF.
* 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
ASSIGN lo_data->* TO <f_line>.
Regards,
Naimesh Patel