Skip to Content
author's profile photo Former Member
Former Member

Field-Symbols as Method Parameter

Hi all,

is there a way to create a field symbol within a method and export this field-symbol directly as a method parameter?

The problem is that we can't pass a field symbol as method parameter unless it's assigned, but we would like to assign the field symbol within a method and give the result back to the calling program.

We have managed to create a field symbol within the method and give back the result as a reference (we used the type ANY).

We would like to pass the result field-symbol directly as a Method parameter.

Any ideas?

Best regards Dominik

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Feb 02, 2009 at 02:34 PM

    Hi Dominik,

    Yes you are right, you can't pass an unassigned field symbol, but you can first assing it a dummy value like in code below

    CLASS cl DEFINITION.
      PUBLIC SECTION.
    
        METHODS meth CHANGING field_symb TYPE any.
    ENDCLASS.                    "cl DEFINITION
    
    CLASS cl IMPLEMENTATION.
      METHOD meth.
        FIELD-SYMBOLS <fs_int> TYPE ANY.
        DATA: field(10) TYPE c VALUE 'SOME_FIELD'.
    
        ASSIGN field TO <fs_int>.
        field_symb = <fs_int>.
      ENDMETHOD.                    "meth
    ENDCLASS.                    "cl IMPLEMENTATION
    
    START-OF-SELECTION.
    
      DATA: ref TYPE REF TO cl,
            dummy_field(10) TYPE c.        
      FIELD-SYMBOLS <fs_out>.
    
      CREATE OBJECT ref.
      ASSIGN dummy_field TO <fs_out>.      "initialize <fs_out> with dummy data
    
      ref->meth( CHANGING field_symb = <fs_out> ).    "now <fs_out> can be passed and changed within the method
      WRITE: <fs_out>.
    

    Please also note that although not obligatory, you should pass it as changing parameter as you change its content inside the method.

    Regards

    Marcin

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 02, 2009 at 09:06 PM

    Hello Dominik

    An alternate approach would be to return a DATA reference.

    CLASS cl DEFINITION.
      PUBLIC SECTION.
     
        METHODS meth RECEIVING rdo_data  TYPE REF TO data.
    ENDCLASS.                    "cl DEFINITION
     
    CLASS cl IMPLEMENTATION.
      METHOD meth.
        FIELD-SYMBOLS <fs_int> TYPE ANY.
        DATA: field(10) TYPE c VALUE 'SOME_FIELD'.
     
        ASSIGN field TO <fs_int>.
        
        GET REFERENCE OF <fs_int> INTO rdo_data.
      ENDMETHOD.                    "meth
    ENDCLASS.                    "cl IMPLEMENTATION
     
    START-OF-SELECTION.
     
      DATA: ref TYPE REF TO cl,
        gdo_data     TYPE REF TO data,
            dummy_field(10) TYPE c.        
      FIELD-SYMBOLS <fs_out>.
     
      CREATE OBJECT ref.
      ASSIGN dummy_field TO <fs_out>.      "initialize <fs_out> with dummy data
     
    "  ref->meth( CHANGING field_symb = <fs_out> ).    "now <fs_out> can be passed and changed within the method
    
    gdo_data = ref->meth( ).
    IF ( gdo_data IS BOUND ).
       ASSIGN gdo_data->* TO <fs_out>.
    
      WRITE: <fs_out>.
    ENDIF.
    
    

    Regards

    Uwe

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 16, 2009 at 12:27 PM

    Hi all,

    thanks for you help. I managed to do the method calls with reference parameters but I'd like to do it with fieldsymbols instead.

    Your suggentions for field-symbold worked, but I could use them only with primitive datatypes.

    In my requirements the table must be created within the method, so the caller doesn't know the tablestructure.

    If a dummy table is assigned to the field-symbol before the call, the error "Two internal tables are neither compatible nor convertible" is produced.

    Is there a solution?

    Thanks in advance

    Dominik

    class ZZX0_CL_TEST definition
      public
      final
      create public .
    
    public section.
    
      class-methods FIELD_SYMBOLS_TAB_TEST
        changing
          !FS type ANY .
    
    METHOD FIELD_SYMBOLS_TAB_TEST .
    
      DATA: it_t000 TYPE TABLE OF t000.
      FIELD-SYMBOLS <field_symbol> TYPE ANY TABLE.
    
      ASSIGN it_t000 TO <field_symbol>.
      fs = <field_symbol>.
    
      EXIT.
    
    ENDMETHOD.
    
    
    
    REPORT zzx0_mini.
    
    DATA: it_mara  TYPE TABLE OF mara.
    
    START-OF-SELECTION.
    
    * Tabellen test
      FIELD-SYMBOLS: <fs> TYPE STANDARD TABLE.
      ASSIGN it_mara TO <fs>.
    *  ASSIGN it_t000 TO <fs>.
    
    
      CALL METHOD zzx0_cl_test=>field_symbols_tab_test
        CHANGING
          fs = <fs>.
    
      EXIT.
    

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hello Dominik,

      The sample cannot work because the caller uses an internal table with MARA as line type. This perfectly matches the any of the signature. But within the method body tries to set the value to a table with line type T000.

      Possibly the code confuses the point in time when a untyped field-symbol adopts the type information. This happens exclusively during the ASSIGN statement. The the field-symbols points to a data item with a fixed type. When passing a value to the field-symbol later the type of the value must be type compatible.

      In case the requirement is that the type is specified within the method body (the callee and not the caller) data references are the most common solution. In most cases this teams up with CREATE DATA. A data reference to local variables on the stack would get invalid by leaving the method.

      method get_Table. " returning value(result) type ref to data
        data:
          local_Table type standard table of t000.
        field-symbols: <table> like local_Table.  
        
        create data result like local_Table.
        assign result->* to <table>.
        insert initial line into table <table>.
      endmethod.
      
      

      Hope this helps

      Klaus

  • author's profile photo Former Member
    Former Member
    Posted on Jul 12, 2010 at 01:45 AM

    problem understood, can't be solved

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.