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

Get type of variable dynamically

Former Member
0 Likes
8,642

Hi,

i want to get the type of a variable. For example, the following code :

data: my_var type zmystruct.

In ABAP, i want to get type 'zmystruct' and after to create dynamically a new variable of this type.

Thanks for help.

5 REPLIES 5
Read only

raja_thangamani
Active Contributor
0 Likes
3,708

HI


**** Get type object from a data object


DATA:   DATATYPE TYPE REF TO CL_ABAP_DATADESCR,
        FIELD(5) TYPE C.

DATATYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( FIELD ).

* For stucture

DATA: LINETYPE  TYPE REF TO CL_ABAP_STRUCTDESCR,
      MYSTRUC TYPE SPFLI.

* Get Structure of Some Database Table for example

LINETYPE ?=   CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( MYSTRUC ).

Write: DATATYPE->TYPE_KIND,
       LINETYPE->TYPE_KIND,
       LINETYPE->ABSOLUTE_NAME.

Raja T

Message was edited by:

Raja T

Read only

Former Member
0 Likes
3,708

see also keyword DESCRIBE FIELD

Read only

Former Member
0 Likes
3,708

HI,

You use the following syntax to define reference types:

<b>TYPES dtype {TYPE REF TO type}|{LIKE REF TO dobj}.</b>

The syntax for a direct declaration of a reference variable is as follows:

<b>

DATA ref {TYPE REF TO type}|{LIKE REF TO dobj}.</b>

The addition REF TO defines a data type dtype for a reference variable and declares the reference variable ref. The specification after REF TO specifies the static type of the reference variable. The static type constricts the object set to which a reference variable can point. The dynamic type of a reference variable is the data type and the class of the object respectively, to which it currently points. The static type is always more universal or equal to the dynamic type

You can use the TYPE addition to define data types for data and object reference variables. The LIKE addition exclusively defines data types for data reference variables.

The syntax and meaning of the additions TYPE and LIKE are completely equal for both statements with the exception that TYPES creates an independent reference type, whereas DATA creates a bound reference type.

Regards

Sudheer

Read only

Former Member
0 Likes
3,708

There's a very good tutorial on creating dynamic objects here. It will take a few minutes to get through it, but it is worth the read.

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/b332e090-0201-0010-bdbd-b73...

Read only

former_member186741
Active Contributor
0 Likes
3,708

if it really is a simple case of creating variables like others then you can do this:

DATA my_dref TYPE REF TO data.

FIELD-SYMBOLS <struc> TYPE ANY.

CREATE DATA my_dref type zmystruct.

ASSIGN my_dref->* TO <struct>.

*Field symbol <struct> is now an instance of zmystruct.

or

CREATE DATA my_dref like my_var.

ASSIGN my_dref->* TO <struct>.