Skip to Content

how to declare a fully typed field-symbol based on TYPE REF TO DATA parameter ?

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Jul 29, 2020 at 06: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.
    
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 29, 2020 at 07: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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 29, 2020 at 07:35 PM
    Here is my understanding of your design struggle:
    • You have a single validate routine working on data of different types dynamically.
    • You want to change it to a number of data types (classes) each overriding the method validate, i.e. many implementations of the same method.

    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

    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.