‎2009 May 25 7:21 AM
Hi,
SAP doesnt allow me to declare a class attribute with Type - STANDARD TABLE or ANY TABLE. However, it allows me to use them in the parameters of their methods. Is there any work around to achieve this ?
Murali.
‎2009 May 25 8:41 AM
Create an attribute of type REF TO DATA. eg. p_anything TYPE REF TO DATA.
Then you can have a method which takes an importing parameter (changing if you're going to change it) of TYPE ANY. e.g. i_something TYPE ANY.
In the method
GET REFERENCE OF i_something INTO me->p_anything.Now, in another method if you need to do something with the data, use
FIELD-SYMBOLS: <l_something> TYPE ANY.
ASSIGN me->p_anything->* TO <l_something>.Field symbol <l_something> contains the same data as what was in i_something.
I use this technique frequently.
matt
‎2009 May 25 8:26 AM
Hi,
SAP doesnot allow generic types for attributes of classes is because these attributes will be used in the methods of the classes and it would not give a clear picture to the methods of which type they are referring to....
Using generic types as an attribute leads to confusion, so the best way to use generic type is to pass them using the parameters of the methods rather than declaring them as an attribute of the class....
Regards,
Siddarth
‎2009 May 25 8:41 AM
Create an attribute of type REF TO DATA. eg. p_anything TYPE REF TO DATA.
Then you can have a method which takes an importing parameter (changing if you're going to change it) of TYPE ANY. e.g. i_something TYPE ANY.
In the method
GET REFERENCE OF i_something INTO me->p_anything.Now, in another method if you need to do something with the data, use
FIELD-SYMBOLS: <l_something> TYPE ANY.
ASSIGN me->p_anything->* TO <l_something>.Field symbol <l_something> contains the same data as what was in i_something.
I use this technique frequently.
matt
‎2009 May 25 8:44 AM
Hello Murali
One possible workaround would be to use a DATA object instead of the untyped TABLE parameter.
" Example: Class attribute (e.g. MDO_DATA) is used for reading
" Fill the attribute within one of your class methods:
METHOD xyz.
...
GET REFERENCE OF lt_knb1 INTO me->mdo_data.
ENDMETHOD.
" Read the class attribute:
FIELD-SYMBOLS:
<lt_itab> TYPE TABLE.
ASSIGN go_myclass->mdo_data->* TO <lt_itab>.
The same way can be used the other way around to fill the class attribute:
DATA:
lt_knb1 TYPE STANDARD TABLE OF knb1.
SELECT * FROM knb1 INTO TABLE lt_knb1.
GET REFERENCE OF lt_knb1 INTO go_myclass->mdo_data.
Regards
Uwe
‎2009 May 28 7:06 AM
‎2009 May 25 11:18 AM
‎2023 Mar 27 1:28 PM
This worked for me. First create class attribute with TYPE REF TO DATA (in our case attribute name is mp_test ).
Then in setter method do this (parameter it_table is TYPE ANY TABLE.):
FIELD-SYMBOLS:
<lt_table> TYPE ANY TABLE.
CREATE DATA mp_test LIKE it_table.
ASSIGN mp_test->* TO <lt_table>.
IF sy-subrc = 0.
<lt_table> = it_table.
ENDIF.
Then in anywhere else you can access mp_test like this:
FIELD-SYMBOLS:
<lt_table> TYPE ANY TABLE.
ASSIGN me->mp_test->* TO <lt_table>.
‎2023 Mar 27 1:58 PM
So you are basically using dereferencing which matthew.billingham posted in the first/best answer way back in 2009. Thanks for raising zombies!