ABAP 给动态变量赋值

【转自 http://blog.csdn.net/forever_crazy/article/details/6544830】

需求: 有时写程序的时候,需要给某个动态变量 赋值操作,当字段比较多时,如果用常规方法赋值 就会显得代码很冗余,其实可以用变量的间接寻址赋值。

ex1: 

data    c1    type    char10.

data    c2     type    char10.

field-symbols  <fs>  type  any.

c1  = 'C2'.             "注意:此时的这个要赋值的变量“C2”一定要是大写的,因为在abap内部    变量名都是以大写格式保存的。

c2 = 'test1'.

assign  (c1)  to  <fs>.

<fs>  = 'test2'.

这时变量c2的内容由 'test1'  ->   'test2'.

该种方法适用于 多个变量要进行动态赋值时使用。

下面例子说明:点下那个按钮就将那个按钮文本设为'test'.

data  ok_code      type   sy-ucomm.

data  l_but           type    char10.

field-symbols  <fs> type any.

parameters:

    pushbutton  2(10)    pb1   user-command pb1,

    pushbutton  12(10)  pb2   user-command pb2,

    pushbutton  22(10)  pb3   user-command pb3,

    pushbutton  32(10)  pb4   user-command pb4.

at selection-screen.

   ok_code = sy-ucomm.

   l_but = ok_code.

   assign (l_but)  to  <fs>.

   <fs> = 'test'.

该方法减少了对每个按钮都要进行判断然后赋值,减少了代码的冗余。

原文地址:https://www.cnblogs.com/rainysblog/p/4143382.html