Application Development 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: 

Dynamic challenge

Former Member
0 Kudos

Hi,

I have a question that is affecting my project delivery :

I have below structure and i want to change the data type of addr field to f or n DYNAMICALLY during runtime.

TYPES : BEGIN OF st_demo,

reg_no(10) TYPE c,

name(20) TYPE c,

addr(20) TYPE c,

END OF st_demo.

DATA : wa_demo TYPE st_demo,

it_demo TYPE TABLE OF st_demo,

Can anyone guru in abap advice me.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Dear MercAMG,

sure.

This is an easy one.

You need to use FIELD SYMBOLS.... don't be afraid of them.

;o)

Here's the code with explanations.


REPORT  zrq.
* Ricardo Quintas 2011/09

* Here you declare your 2 field symbols
FIELD-SYMBOLS <field_n> TYPE n.
FIELD-SYMBOLS <field_d> TYPE d.

TYPES : BEGIN OF st_demo,
            reg_no(10) TYPE c,
            name(20) TYPE c,
            addr(20) TYPE c,
END OF st_demo.

* Now you declare your line type
DATA : wa_demo TYPE st_demo.

* And you choose dinamically what data type you want to assign 
* your addr field.
* (the IF-ENDIF is just an example)
IF bla bla bla...
  ASSIGN wa_demo-addr TO <field_n> CASTING.
ELSE.
  ASSIGN wa_demo-addr TO <field_d> CASTING.
ENDIF.

* And finally you declare your internal table, with your 
* dinamically created line type. Pay attention to the "LIKE" instead of "TYPE"  
DATA: it_demo LIKE TABLE OF wa_demo.

Just to let you know that you might get errors at run-time if you don't take care of data conflicts.

You've been warned. ;o)

Kind Regards

/Ricardo Quintas

2 REPLIES 2

Former Member
0 Kudos

Dear MercAMG,

sure.

This is an easy one.

You need to use FIELD SYMBOLS.... don't be afraid of them.

;o)

Here's the code with explanations.


REPORT  zrq.
* Ricardo Quintas 2011/09

* Here you declare your 2 field symbols
FIELD-SYMBOLS <field_n> TYPE n.
FIELD-SYMBOLS <field_d> TYPE d.

TYPES : BEGIN OF st_demo,
            reg_no(10) TYPE c,
            name(20) TYPE c,
            addr(20) TYPE c,
END OF st_demo.

* Now you declare your line type
DATA : wa_demo TYPE st_demo.

* And you choose dinamically what data type you want to assign 
* your addr field.
* (the IF-ENDIF is just an example)
IF bla bla bla...
  ASSIGN wa_demo-addr TO <field_n> CASTING.
ELSE.
  ASSIGN wa_demo-addr TO <field_d> CASTING.
ENDIF.

* And finally you declare your internal table, with your 
* dinamically created line type. Pay attention to the "LIKE" instead of "TYPE"  
DATA: it_demo LIKE TABLE OF wa_demo.

Just to let you know that you might get errors at run-time if you don't take care of data conflicts.

You've been warned. ;o)

Kind Regards

/Ricardo Quintas

0 Kudos

I resolved it myself using RTTI but thanks for advicing