02-02-2009 1:09 PM
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
02-02-2009 2: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
02-02-2009 2: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
02-02-2009 9: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
11-17-2012 11:53 AM
An alternate approach would be to return a DATA reference.
This worked for me, thanks
02-16-2009 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.
02-16-2009 1:40 PM
Hello Dominik,
there seems to be a glitch in your code. The method parameter is typed as any, but within the method body the code is only able to work with a certain table type. That cannot work (and is not related to field-symbols).
The use generic types in the signature implies a less strict type check by the compiler, but the ABAP runtime will abort or raise exceptions for not matching data types. The overall code of caller and callee needs to reflect this.
Regards
Klaus
02-16-2009 1:48 PM
Hi Klaus,
thanks for you quick reply.
Unfortuanetly I don't get what to do now.
Declaring the method parameter as STANDART TABLE doesn't help.
Is there a solution or did you just give me an explanation why my approach does not work.
Best regards Dominik
02-16-2009 2:11 PM
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
07-12-2010 2:45 AM