07-29-2020 5:34 PM
Hi Folks, this is driving me crazy, can someone enlight me.
I have this code
Definition
Class-Methods: validate_data IMPORTING t_data TYPE REF TO DATA.
Implementation
METHOD validate.
TYPES: type_ztabname TYPE TABLE OF ztabname.
FIELD-SYMBOLS: <fully_typed_table> TYPE type_ztabname.
ASSIGN t_data->* TO <fully_typed_table>.
* then it uses <fully_typed_table>-fieldname because the field-symbol is fully typed
* validations
ENDMETHOD.
This is in a class with lots of method just like this one (one for each table they want to validate)
I want to change it and make it better creating a subclass for each table than inherit from an Asbtract class where I pass the table name to the constructor and in each method I can deal with the data without having to repeat the table again .
The problem is that I can't find a way to have a fully type <fs_table> in the method without defineing the type in the same scope. I know the tablename (I pass it to the constructor) I don't want in each method declare the type with again the same table name. But also I need the <fully_typed_table> to be fully typed. I cant change all the <fully_typed_table>-fieldname.
FIELD-SYMBOLS: <fully_typed_table> TYPE (me->table_type). "me->table_type = 'ZTABNAME'
Is there a way to do this ?
Thanks
07-29-2020 7:44 PM
First of all, sorry, but your <fully_typed_table> is not fully typed !
A table type fully typed (i.e. not generic) has to have a table category and a key. Example:
TYPES: type_ztabname TYPE STANDARD TABLE OF ztabname WITH EMPTY KEY.
With a type fully typed, you may create a typed data reference:
DATA ref_table TYPE REF TO type_ztabname.
And it's easier to work with:
METHOD validate.
ref_table ?= t_data.
LOOP AT ref_table->* REFERENCE INTO DATA(ref_line).
DATA(fieldname) = ref_line->fieldname.
ENDLOOP.
ENDMETHOD.
07-29-2020 7:44 PM
First of all, sorry, but your <fully_typed_table> is not fully typed !
A table type fully typed (i.e. not generic) has to have a table category and a key. Example:
TYPES: type_ztabname TYPE STANDARD TABLE OF ztabname WITH EMPTY KEY.
With a type fully typed, you may create a typed data reference:
DATA ref_table TYPE REF TO type_ztabname.
And it's easier to work with:
METHOD validate.
ref_table ?= t_data.
LOOP AT ref_table->* REFERENCE INTO DATA(ref_line).
DATA(fieldname) = ref_line->fieldname.
ENDLOOP.
ENDMETHOD.
07-29-2020 8:08 PM
Hi, sandra.rossi. Yeah you are right, is not fully typed
TYPES: type_ztabname TYPE STANDARD TABLE OF ztabname WITH EMPTY KEY.
What I was trying to ask is if there is a way to replace the bold part with the content of a variable, but now I realice that is just stupid because even if that would be possible the compiler would never know the structure until runtime so the use of <fs>-fieldname or ref_line->fieldname is impossible.
I just got burnout, my bad. nvm
07-30-2020 9:03 AM
Thanks for the feedback!
Please use the COMMENT button for comments, questions, adding details, replying to OP comment, etc., ANSWER is only to propose a solution, dixit SAP text at the right of the answer area.
07-29-2020 8:35 PM
I do not agree that this transformation makes it better. It depends on the context. Actually the first solution is canonical ABAP.
The second solution is easier in a functional language (like javascript) where you would pass a first class function to the validate method. In ABAP OO, I would suggest using an interface (strategy pattern) instead of your proposed subclassing (template pattern). It will be verbose as you have to objectify (create a class for <fully_typed_table>) and use at least a factory method to inject the object) instead of using dynamic data references.
my 2 cents
JNN