cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamic assignment variant for Field-symbols not allowed

Juwin
Active Contributor
0 Kudos
771

I have a class in which I have defined various Text elements.
For eg.
H01=Title1
H02=Title2
H03=Title3
H04=Title4
etc.
I am trying to print all the titles in my class. I have a loop in my class and I am building the text element name dynamically.

lv_title = `TEXT-H` && conv ty_n2( sy-tabix ).

Now, I use this variable to dynamically access the text element in the program

assign (lv_title) to <title>.

Class is syntactically correct and executes fine with expected results.

But, I get a warning saying: "The old variant of "<dynamic-object>" should not be used in the current ABAP language version."

If this is considered the "old" variant, what is the equivalent "new" variant to access variables by name?

I am on ABAP Cloud version, so that limits the commands available to me (for eg. READ TEXTPOOL isn't available to me)

Accepted Solutions (1)

Accepted Solutions (1)

Sandra_Rossi
Active Contributor

No pragma exists to remove the warning but you may rewrite your code to avoid field symbols.

As per the ABAP Cloud - ABAP Keyword Documentation: "This syntax is only supported temporarily in the current ABAP language version and must be replaced with valid syntax for that version [...] This variant should not be used any more. Especially for accessing structure components, other variants are preferable."

If you have a structure, use ASSIGN COMPONENT ... OF STRUCTURE ...

That's not possible for the pseudo structure TEXT of Symbol Texts, but you may rewrite this way which is more clean code (of course, I don't know what your real case is, so I use a dummy example):

Sandra_Rossi_0-1740730679507.png

CLASS zcl_test DEFINITION
  PUBLIC FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_test IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    TYPES:
      BEGIN OF ty_month,
        astrological_sign TYPE string,
      END OF ty_month.
    TYPES ty_n2 TYPE n LENGTH 2.

    DATA months   TYPE TABLE OF ty_month.
    DATA lv_title TYPE string.

    " Original code
    DATA(month) = months[ astrological_sign = 'Lion' ].
    lv_title = `TEXT-H` && CONV ty_n2( sy-tabix ).
    ASSIGN (lv_title) TO FIELD-SYMBOL(<title>).

    " Suggested clean code
    DATA(month_number) = line_index( months[ astrological_sign = 'Lion' ] ).
    DATA(month_name) = SWITCH string( month_number
                                      WHEN 1 THEN 'January'(H01)
                                      WHEN 2 THEN 'February'(H02)
                                      " Etc.
                                    ).
  ENDMETHOD.
ENDCLASS.

 

Answers (3)

Answers (3)

RaymondGiuseppi
Active Contributor
0 Kudos

Why don't you use RS_TEXTPOOL_READ 

 

    lv_classn = iv_class.
    lv_classn+30(2) = 'CP'.
    TRANSLATE lv_classn USING ' ='.
    lv_programm = lv_classn.

    CALL FUNCTION 'RS_TEXTPOOL_READ'
      EXPORTING
        objectname           = lv_programm
        action               = 'SHOW'
        language             = iv_langu
      TABLES
        tpool                = lt_table
      EXCEPTIONS
        object_not_found     = 1
        permission_failure   = 2
        invalid_program_type = 3
        error_occured        = 4
        action_cancelled     = 5.

 

Or you could use the READ TEXTPOOL statement followed by some clean ASSIGN syntax for the type 'I' (text symbols)

Sandra_Rossi
Active Contributor
0 Kudos
It's fine for classic ABAP but it's not accepted in ABAP Cloud, the function module is not released and the ABAP statement is not ported to Cloud. XCO doesn't currently permit working with text pools as far as I can see.
RaymondGiuseppi
Active Contributor
0 Kudos
Is the utility class CL_FDT_SERVICES available?
Sandra_Rossi
Active Contributor
0 Kudos
No, CL_FDT_SERVICES is not released either. I checked in BTP ABAP environment trial. I guess it's the same status in S/4HANA Cloud.
Mar91
Product and Topic Expert
Product and Topic Expert
0 Kudos

Ciao Juwin.

The problem is that TEXT is not a structure so these suggested  variants cannot be used.

... { struc-(comp) } 
  | { dref->(comp_name) } 
  | { COMPONENT comp OF STRUCTURE struc } ...

I suggest to declare a proper structure like my_text and to put or duplicate the text symbol's values there as subcomponents.

Juwin
Active Contributor
0 Kudos
I don't want to duplicate the text symbol values. I want to just access the existing values.
shantraj
Explorer
0 Kudos

You can use left = right 

something like this -----   <title> = lv_title.

Juwin
Active Contributor
0 Kudos
Probably you didn't understand my question. Please read through once more.