cancel
Showing results for 
Search instead for 
Did you mean: 

Passing an internal table WITH HEADER LINE to abap object

Former Member
0 Kudos
2,247

Hi. In another thread, it was explained how to pass an internal table to an abap object method. Is it possible to pass an internal table that has a header line, and RETAIN the header line once the table has been passed?

My problem is, that I can pass the table, update it, but the read buffer is not populated when returning from the object's method. This is the result of being able to pass a STANDARD TABLE type, but not a STANDARD TABLE WITH HEADER LINE.

This means that I have to read the table into a work area instead of doing a READ TABLE LKNA1 within the method, which is what I need to do.

Thanks.

Accepted Solutions (0)

Answers (4)

Answers (4)

RichHeilman
Developer Advocate
Developer Advocate

No, header lines are not allow in OO. You must use a work area.

data: xkna1 tye kna1.

read table lkna1 into xkna1.

Regards,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is an example program, passing internal table to class.



report zrich_0001.


*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.

    types: t_t001 type table of t001.

    class-data: it001 type table of t001.
    class-data: xt001 like line of it001.


    class-methods: fill_table importing im_t001 type t_t001,
                   write_table.

endclass.


data: a_t001 type table of t001.

start-of-selection.

  select * into table a_t001 from t001.
  call method lcl_app=>fill_table( a_t001 ).
  call method lcl_app=>write_table.


*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.

  method fill_table.

    it001 = im_t001.

  endmethod.

  method write_table.
    loop at it001 into xt001.
      write:/ xt001-bukrs, xt001-butxt.
    endloop.
  endmethod.

endclass.

Regards,

Rich Heilman

Former Member
0 Kudos

Thanks, Rich. That is definitely a better solution than what I had before, but I have one final question about your idea...

I noticed that I could define a parameter of type LINETYPE. Is there a way to make the second parameter (the "buffer" parameter) more generic? If I make the parameter type LINETYPE, then there is a type conflict when I attempt to pass LKNA1 (in my example). If I change the parameter type of the method to KNA1, it works, but that couples the "buffer" parameter to the KNA1 structure type.

If I could somehow pass LKNA1 as a LINETYPE, and then assign it to a KNA1 type inside of the object method, then the solution would be clean.

Thanks.

Former Member
0 Kudos

Brendan,

you can do it:

loop at <table> into <wa>.

or

loop at <table> assigning <field_symbol>.

..you work with <wa> or <field_symbol>

Modify <table> from <wa> index...

endloop.

regards,

Alexandre

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Please check this sample program, notice that it is modifing the internal table and passing it back modified as well as passing the "work area" or "header line" back thru the exporting parameter.



report zrich_0001.


*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.

    types: t_t001 type table of t001.

    class-data: it001 type table of t001.
    class-data: xt001 like line of it001.


    class-methods: change_table
                                exporting ex_wt001 type t001
                                changing im_t001 type t_t001.


endclass.

data: w_t001 type t001.
data: a_t001 type table of t001 with header line.

start-of-selection.

  select * into table a_t001 from t001.
  call method lcl_app=>change_table
             importing
                 ex_wt001 = w_t001
             changing

                 im_t001  = a_t001[] .

  check sy-subrc  = 0.


*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.

  method change_table.

    loop at im_t001 into xt001.
      concatenate xt001-butxt 'Changed'
           into xt001-butxt separated by space.
      modify im_t001 from xt001.
    endloop.

    ex_wt001 = xt001.

  endmethod.

endclass.

Regards,

Rich Heilman

Former Member
0 Kudos

Thanks for replying, Rich.

The thing is, I need to pass the table as a CHANGING parameter so that I can update it. When control returns to the caller, I need to have the table's read buffer populated when it comes back from the method. For example, I have the following specified where the itab parameter is of type STANDARD TABLE:

data: begin of lkna1 occurs 0001.      
      include structure kna1.          
data: end of lkna1.                    

* .. Table gets populated here. Now pass table to object:

create object ztestobj.                        
                                              
call method ztestobj->settable                 
            changing itab = lkna1[].

* I have to add the following lines after this method
* call because I cannot just do a "read table kna1."
* to populate the read buffer from within the method.
* I would have to "read table kna1 into wa_kna1" within
* the method, which does not help. This is not good, as I
* need to completely encapsulate the implementation:
                                              
read table lkna1.                            
if sy-subrc = 0.                              
endif.

Thanks.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Which record do you still want to be in the "read buffer"? First, Last, which?

Regards,

Rich Heilman