Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Type ANY in Class Attribute

Murali_Shanmu
SAP Champion
SAP Champion
0 Likes
9,194

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.

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
4,305

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

7 REPLIES 7
Read only

Former Member
0 Likes
4,305

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

Read only

matt
Active Contributor
4,306

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

Read only

uwe_schieferstein
Active Contributor
0 Likes
4,305

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

Read only

0 Likes
4,305

Thanks a lot people.

Read only

Former Member
0 Likes
4,305

This message was moderated.

Read only

0 Likes
4,305

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>.
Read only

4,305

So you are basically using dereferencing which matthew.billingham posted in the first/best answer way back in 2009. Thanks for raising zombies!