Application Development 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: 

Work area Inside a method in Object Oriented Programming

Former Member
0 Kudos

Hi all,

Can anyone look at the below code in the method meth1 and tell me is there any better method rather than declaring the workarea wa_itab using t_mara.

I can use field symbols but can i use access one field like if i say <fs>-matnr it doesnot let me do that

I don't want to declare struture t_mara just to declare work area wa_itab. i know i can declare using field symbols like

FIELD-SYMBOLS:<fs> type any. but how do i acess an individual field

CLASS cls DEFINITION.

PUBLIC SECTION.

TYPES:BEGIN OF T_MARA,

MATNR TYPE MARA-MATNR,

meins TYPE MARA-meins,

END OF T_MARA.

data : WA_ITAB TYPE T_MARA.

METHODS:meth1 importing itab type any table.

ENDCLASS.

CLASS cls IMPLEMENTATION.

METHOD meth1.

*FIELD-SYMBOLS:<fs> type itab."any.

loop at itab into wa_itab. "assigning <fs>.

write:/ wa_itab-matnr,wa_itab-meins.

endloop.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA:obj TYPE REF TO cls.

CREATE OBJECT obj.

TYPES:BEGIN OF T_MARA1,

MATNR TYPE MARA-MATNR,

meins TYPE MARA-meins,

END OF T_MARA1.

DATA:ITAB1 TYPE STANDARD TABLE OF T_MARA1,

WA_ITAB1 TYPE T_MARA1.

SELECT matnr meins FROM mara into TABLE itab1 up to 10 rows.

CALL METHOD obj->meth1 exporting itab = itab1.

Thanks

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

I rewrote your code.

Instead of declaring type again in the Program, I have created a table type and a public attribute IT_MARA which I have used to select the data and passed to my method to write the data.


*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
CLASS CLS DEFINITION.
  PUBLIC SECTION.
    TYPES:BEGIN OF T_MARA,
          MATNR TYPE MARA-MATNR,
          MEINS TYPE MARA-MEINS,
          END OF T_MARA.

    TYPES: TY_T_MARA TYPE STANDARD TABLE OF T_MARA.  " <<

    DATA: IT_MARA TYPE TY_T_MARA.   " <<

    METHODS:METH1
      IMPORTING
        ITAB TYPE TY_T_MARA.  "<<

ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS CLS IMPLEMENTATION.
  METHOD METH1.

    DATA: LA_ITAB LIKE LINE OF ITAB.

    LOOP AT ITAB INTO LA_ITAB.
      WRITE:/ LA_ITAB-MATNR,LA_ITAB-MEINS.
    ENDLOOP.
  ENDMETHOD.                                                "meth1

ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.

  DATA:OBJ TYPE REF TO CLS.
  CREATE OBJECT OBJ.

  SELECT MATNR MEINS
    FROM MARA
    INTO TABLE OBJ->IT_MARA UP TO 10 ROWS.

  CALL METHOD OBJ->METH1
    EXPORTING
      ITAB = OBJ->IT_MARA.

Regards,

Naimesh Patel

8 REPLIES 8

naimesh_patel
Active Contributor
0 Kudos

I rewrote your code.

Instead of declaring type again in the Program, I have created a table type and a public attribute IT_MARA which I have used to select the data and passed to my method to write the data.


*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
CLASS CLS DEFINITION.
  PUBLIC SECTION.
    TYPES:BEGIN OF T_MARA,
          MATNR TYPE MARA-MATNR,
          MEINS TYPE MARA-MEINS,
          END OF T_MARA.

    TYPES: TY_T_MARA TYPE STANDARD TABLE OF T_MARA.  " <<

    DATA: IT_MARA TYPE TY_T_MARA.   " <<

    METHODS:METH1
      IMPORTING
        ITAB TYPE TY_T_MARA.  "<<

ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS CLS IMPLEMENTATION.
  METHOD METH1.

    DATA: LA_ITAB LIKE LINE OF ITAB.

    LOOP AT ITAB INTO LA_ITAB.
      WRITE:/ LA_ITAB-MATNR,LA_ITAB-MEINS.
    ENDLOOP.
  ENDMETHOD.                                                "meth1

ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.

  DATA:OBJ TYPE REF TO CLS.
  CREATE OBJECT OBJ.

  SELECT MATNR MEINS
    FROM MARA
    INTO TABLE OBJ->IT_MARA UP TO 10 ROWS.

  CALL METHOD OBJ->METH1
    EXPORTING
      ITAB = OBJ->IT_MARA.

Regards,

Naimesh Patel

0 Kudos

Thanks

Can you also tell me TABLE OBJ->IT_MARA doesit create an internal table runtime

And the select query should be always after the create object statement right.

It solves my probelm

Just out of curiousity i'm asking again that it is possible to access something like this <fs>-matnr if we declare

field symbols : <fs> type any

Let me know if i'm wrong

Thanks

0 Kudos

Just out of curiousity i'm asking again that it is possible to access something like this <fs>-matnr if we declare

field symbols : <fs> type any

No, it is not. Because the field symbol does not have an explicit type, so you can not access its components directly. You would have to assign the component to a field symbol as well.

field symbols : <fs> type any
field symbols : <fs_matnr> type any

ASSIGN COMPONENT 'MATNR' of structure <fs> to <fs_matnr>.

Rich Heilman

0 Kudos

If you decalre your importing parameter as generic (TYPE ANY TABLE) than it will give you syntax error.


    METHODS:METH1
      IMPORTING
        ITAB TYPE any table. 

  METHOD METH1.

    field-symbols: <f1> like line of itab.
  ENDMETHOD.                                                "meth1

The line type of "ITAB" is generic. already been defined with "DATA".

Regards,

Naimesh Patel

0 Kudos

Hi David,

Firstly the TABLES statement creates a work area and not a internal table.

Moreover this statement is not allowed in OOABAP....

As regards your second query...it wont be possible to access DIRECTLY individual fields of a field symbol which is declared as field-symbols <fs> type any.

In such cases you need to use the statements

ASSIGN COMPONENT {NUMBER | FIELDNAME} OF STRUCTURE <FS> TO <FS_FIELD>

ex.

FIELD-SYMBOLS <FS_FIELD> TYPE ANY.

ASSIGN COMPONENT 'MATNR' OF STRUCTURE <FS> TO <FS_FIELD>

~Piyush Patil

0 Kudos

Thanks Everyone

I awarded points to all

Thanks again

Former Member
0 Kudos

try declaring the field symbols as field-symbol <fs> like line of itab. With this you will be able to access all fields of itab..

0 Kudos

Hi Piyush,

As per your suggetsion i have to declare the struture again in Class.

I don't want to declare again in class i already did in Program

Thanks