‎2006 Dec 29 4:35 AM
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?
‎2006 Dec 29 5:14 AM
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.
‎2006 Dec 29 5:02 AM
Hi,
Use field-symbols.
FIELD-SYMBOLS <fs>.
Check this link.
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/field_sy.htm
‎2006 Dec 29 5:07 AM
Hi,
Chk this link...
https://www.sdn.sap.com/irj/sdn/abap-elearning
Regards,
Balaji Reddy G
***Rewards if answers are helpful
‎2006 Dec 29 5:14 AM
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.
‎2006 Dec 29 6:08 AM
Thanks Larry ... It was helpful indeed and solved our problem.