Monday, August 6, 2007

Assign Variable Type at Runtime

Dynamic programing can save a lot of 'superfluous' coding.

To assign a variable a type at runtime, use the ABAP statement ASSIGN with the option TYPE.

For instance:

DATA: D_TYPE,
D_FIELD(35).
DATA: D_TEST TYPE P.
FIELD-SYMBOLS: .
D_FIELD = 'D_TEST'.
D_TYPE = 'P'.
ASSIGN (D_FIELD) TO TYPE D_TYPE.

Additionally you can use the option DECIMALS of the ASSIGN statement if your type is 'P'.
You can even change the type at runtime of previously assigned field symbol like

ASSIGN TO TYPE 'C'.

One more thing for dynamic programing. With the following coding you can access every DDIC table with the key you want:

SELECT SINGLE * FROM (DF_TNAME) INTO S_TMP WHERE (IF_KEY).

No comments: