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

Dynamic variable creation

shreevathsa_s
Product and Topic Expert
Product and Topic Expert
0 Likes
1,224

Is there a way to create a variable whose type is known at runtime.

For Eg.,

DATA: data_type type string value 'CHAR10',

data_value type string value 'ABCDEFGHIJ'

Can we create a variable, var1 of type data_type and value data_value?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
772

Based on your initialization of string types during declaration, I'm assuming that you're working on SAP version 4.7 or higher. If that is indeed the case, this is one way to do it:

data:
   data_type   type string value 'CHAR10',
   data_value  type string value 'ABCDEFGHIJ'.

data:   
   data_ref    type ref to data.

field-symbols:
  <data_value> type any.

* Instantiate a variable of type data_type and store a reference
* to it in data_ref...
  create data data_ref type (data_type).

* Assign the ref to a field symbol.  Now the field symbol represents
* a variable of the type specified in data_type...
  assign data_ref->* to <data_value>.

* (Now you can work with the new variable via the field symbol.)

* Move a value to the new variable...
  <data_value> = data_value.

* Write out the variable's value...
  write: / <data_value>.

Please note that this is a very simplistic example and does not include exception handling, etc. (which you will most likely want to implement).

I hope that this is helpful.

4 REPLIES 4
Read only

jayanthi_jayaraman
Active Contributor
0 Likes
772

Hi,

Use field-symbols.

FIELD-SYMBOLS <fs>.

Check this link.

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

Read only

Former Member
Read only

Former Member
0 Likes
773

Based on your initialization of string types during declaration, I'm assuming that you're working on SAP version 4.7 or higher. If that is indeed the case, this is one way to do it:

data:
   data_type   type string value 'CHAR10',
   data_value  type string value 'ABCDEFGHIJ'.

data:   
   data_ref    type ref to data.

field-symbols:
  <data_value> type any.

* Instantiate a variable of type data_type and store a reference
* to it in data_ref...
  create data data_ref type (data_type).

* Assign the ref to a field symbol.  Now the field symbol represents
* a variable of the type specified in data_type...
  assign data_ref->* to <data_value>.

* (Now you can work with the new variable via the field symbol.)

* Move a value to the new variable...
  <data_value> = data_value.

* Write out the variable's value...
  write: / <data_value>.

Please note that this is a very simplistic example and does not include exception handling, etc. (which you will most likely want to implement).

I hope that this is helpful.

Read only

shreevathsa_s
Product and Topic Expert
Product and Topic Expert
0 Likes
772

Thanks Larry ... It was helpful indeed and solved our problem.