动态声明变量名 [英] Declare variable name dynamically
问题描述
任务是创建具有动态名称的变量.不是类型,而是名称!
向我提出的所有方法(例如通过cl_abap_typedescr
和cl_abap_elemdescr
类)都没有用.
我想在语义上实现这样的东西,但这种语法不正确:
The task is to create variable with dynamic name. Not type, but name!
All the ways proposed to me (e.g. via cl_abap_typedescr
and cl_abap_elemdescr
classes) were useless.
I want to implement semantically something like this, but this syntax is incorrect:
CREATE DATA (name) TYPE var_type.
有什么解决办法吗?
推荐答案
我认为如果将 'name' 声明为字段符号,它会起作用.
I think if 'name' was declared as a field-symbol it would work.
效果该语句声明了一个名为 的符号字段.在运行时,您可以使用 ASSIGN 将具体字段分配给字段符号.使用字段符号执行的所有操作都会直接影响分配给它的字段.
Effect This statement declares a symbolic field called . At runtime, you can assign a concrete field to the field symbol using ASSIGN. All operations performed with the field symbol then directly affect the field assigned to it.
试试这个:
data:
b_1 type i,
b_2 type i,
b_3 type i,
b_4 type i,
num1(1) type n,
fldname type fieldname.
FIELD-SYMBOLS:
<fld> type i.
do 4 times.
num1 = sy-index.
CONCATENATE 'B_' num1 into fldname.
ASSIGN (fldname) to <fld>.
<fld> = sy-index.
enddo.
write: b_1, b_2, b_3, b_4.
这篇关于动态声明变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!