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

How to create dynamic types?

Former Member
0 Likes
633

Hi gurus!

I want to refer on a strucutre, whose components' definitions are available only during runtime.

Example:

The type which i want to refer on should be a structure with one component, which is a CHAR with variable length. (i.e. the VALUE of the component's parameter LENGTH is evaluated during runtime).

Any hints are welcome.

With best regards Jonas

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
612

use cl_abap_elemdescr=>get_c( length )


DATA: r_stru TYPE REF TO cl_abap_structdescr,
      it_comp TYPE cl_abap_structdescr=>component_table,
      r_comp TYPE abap_componentdescr,
      r_elem TYPE REF TO cl_abap_elemdescr,
      r_data TYPE REF TO data.

DATA: length_of_field TYPE i VALUE 10.

FIELD-SYMBOLS: <fs> TYPE ANY.

START-OF-SELECTION.

r_elem = cl_abap_elemdescr=>get_c( length_of_field ).

r_comp-name = 'FIELD1'.
r_comp-type = r_elem.
append r_comp to it_comp.

r_stru = cl_abap_structdescr=>create( it_comp ).

CREATE DATA r_data TYPE HANDLE r_stru.

ASSIGN r_data->('FIELD1') TO <fs>.

<fs> = 'ABC'.

WRITE: / <fs>.

4 REPLIES 4
Read only

Former Member
0 Likes
612

hi, Jonas Soiné,

i think that following code will help you.

____________________________________

TYPES : BEGIN OF my_type,

char(10) TYPE c,

END OF my_type.

DATA my_type_variable TYPE my_type.

my_type_variable-char = 'asdfas'.

WRITE: my_type_variable-char.

____________________________________

Kind Regards,

Faisal

Read only

Former Member
0 Likes
612

Hi Jones,

For your requirement you nedd to play with the Field Symbols.

By Field Symbosl you can assign it to strucuture during runtime.

Please see the link below.

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/field_sy.htm

Thanks,

Chidanand

Read only

Former Member
0 Likes
613

use cl_abap_elemdescr=>get_c( length )


DATA: r_stru TYPE REF TO cl_abap_structdescr,
      it_comp TYPE cl_abap_structdescr=>component_table,
      r_comp TYPE abap_componentdescr,
      r_elem TYPE REF TO cl_abap_elemdescr,
      r_data TYPE REF TO data.

DATA: length_of_field TYPE i VALUE 10.

FIELD-SYMBOLS: <fs> TYPE ANY.

START-OF-SELECTION.

r_elem = cl_abap_elemdescr=>get_c( length_of_field ).

r_comp-name = 'FIELD1'.
r_comp-type = r_elem.
append r_comp to it_comp.

r_stru = cl_abap_structdescr=>create( it_comp ).

CREATE DATA r_data TYPE HANDLE r_stru.

ASSIGN r_data->('FIELD1') TO <fs>.

<fs> = 'ABC'.

WRITE: / <fs>.

Read only

Former Member
0 Likes
612

Cheers friends!

Somehow the last answer was the closest to solve the problem.

But finally i descided not to use dynamical types due to the fact that i would have had to change the whole programm logic without even knowing wether i would recieve better performance.

Anyway thanks J.Soiné