使用构造函数表达式时发生转换异常 [英] Conversion exception while working with constructor expressions

查看:103
本文介绍了使用构造函数表达式时发生转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个例程,该例程将字符串表的行(在本例中为 fui_elements )移动到未知类型的结构( fcwa_struct )。

I'm working on a routine which moves the lines of a string table (in this case fui_elements) into a structure of unknown type (fcwa_struct).

DATA(li_temp) = ... "fill assignment table here

LOOP AT fui_elements ASSIGNING FIELD-SYMBOL(<lfs_element>).

    ASSIGN COMPONENT li_temp[ sy-tabix ] OF STRUCTURE fcwa_struct 
        TO FIELD-SYMBOL(<lfs_field>).

    IF sy-subrc <> 0.
        "somethings wrong with the fui_elements data
    ENDIF.
    <lfs_field> = <lfs_element>.
ENDLOOP.

如果表 i_field_customizing STANDARD TABLE OF string )不是初始值,我想使用其值。

If the table i_field_customizing (STANDARD TABLE OF string) is not initial, I want to use its values.

否则,我想生成一个整数表(因此无论表格的值如何,循环都会同样频繁地运行)。这里 lw_max 是导入的结构具有的字段数:

Otherwise I want to generate an integer table (so that the loop runs equally often regardless of the table's values). Here lw_max is the amount of fields the imported structure has:

DATA(li_temp) = COND #( WHEN i_field_customizing[] IS INITIAL
                        THEN VALUE t_integer_tt( FOR X = 1 
                                                 THEN X + 1 
                                                 WHILE X <= lw_max 
                                                 ( X ) )
                        ELSE i_field_customizing ).






但是当我使用<$ c运行报表时$ c> i_field_customizing 像这样:

DATA(i_field_customizing) = VALUE t_string_tt( ( `KUNNR` ) ( `NAME1` ) ).

在尝试构造 li_temp 的行上遇到此异常code>:

I get this exception on the line where I try to construct li_temp:


CX_SY_CONVERSION_NO_NUMBER(KUNNR不能解释为数字)

CX_SY_CONVERSION_NO_NUMBER (KUNNR cannot be interpreted as a number)






我目前的猜测是 COND 是静态获得的。有人知道我如何解决这个问题吗?


My current guess is that COND gets its type statically. Does anybody know how I can get around this?

推荐答案

您尝试实现的目标将无法实现,因为使用 COND 对变量进行内联定义是在编译时而不是在运行时决定的。

What you are trying to achieve will not be possible because the type of an inline definition of a variable using COND is decided at compilation time and not at runtime.

请参见我的问题<请在href = https://stackoverflow.com/questions/35994332/strange-behaviour-using-string-templates-and-new-cond-syntax>此处。将采用的类型始终是直接位于 THEN 之后的变量的类型。您可以通过在 ELSE THEN 之后否定条件并切换变量位置来决定在编译时选择哪种类型。 c>,但它始终是,或者据我所知,您希望能够动态地做到这一点,因此您的 ASSIGN COMPONENT 语句可以按预期的那样使用整数。

Please see my question here. The type that will be taken is always the type of the variable that stands directly after THEN. You can decide what type will be chosen at compilation time by fiddling with negating the condition and switching places of variables after THEN at ELSE but it will be always either or and from what I understand you want to be able to do it dynamically so your ASSIGN COMPONENT statement works as expected with integers.

即使通过在 ELSE 之后强制转换变量也可以得到与您相同的空转储。

Even by specifically casting the variable after ELSE one gets the same short dump as you do.

DATA(li_temp) = COND #( WHEN i_field_customizing IS INITIAL
                        THEN VALUE t_integer_tt( ( 1 ) ( 2 ) )
                        ELSE CAST t_string_tt( REF #( i_field_customizing ) )->* ).

或者,您可以强制转换为引用数据但是随后必须将其取消引用为 STANDARD TABLE 类型的字段符号。

Alternatively you could cast to REF TO DATA but then you have to dereference it to a field symbol of type STANDARD TABLE.

REPORT zzy.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    TYPES:
      t_integer_tt TYPE STANDARD TABLE OF i WITH EMPTY KEY,
      t_string_tt TYPE STANDARD TABLE OF string WITH EMPTY KEY.
    FIELD-SYMBOLS:
      <fs_table> TYPE STANDARD TABLE.
    DATA: BEGIN OF l_str,
      kunnr TYPE kunnr,
      name1 TYPE name1,
      END OF l_str.

*      DATA(i_field_customizing) = VALUE t_string_tt( ( `KUNNR` ) ( `NAME1` ) ).
      DATA(i_field_customizing) = VALUE t_string_tt( ).

      DATA(li_temp) = COND #( WHEN i_field_customizing IS INITIAL
                              THEN CAST data( NEW t_integer_tt( ( 1 ) ( 2 ) ) )
                              ELSE CAST data( REF #( i_field_customizing ) ) ).

      ASSIGN li_temp->* TO <fs_table>.

      LOOP AT <fs_table> ASSIGNING FIELD-SYMBOL(<fs_temp>).
        ASSIGN COMPONENT <fs_temp> OF STRUCTURE l_str TO FIELD-SYMBOL(<fs_field>).
      ENDLOOP.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).

这篇关于使用构造函数表达式时发生转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆