‎2007 Jul 17 8:19 AM
Hi,
I have code like this:
----
CLASS item DEFINITION
----
........ *
----
class item definition.
public section.
methods:
constructor importing matnr type matnr,
write_result,
get_data.
protected section.
data: matnr type matnr,
maktx type maktx,
groes type groes.
methods: get_maktx,
get_groes.
endclass.
----
CLASS item IMPLEMENTATION
----
........ *
----
class item implementation.
method constructor.
me->matnr = matnr.
call method: get_maktx, get_groes.
call method write_result.
endmethod.
method get_maktx.
select single maktx
from makt into maktx "(me->maktx)
where matnr = matnr.
endmethod.
method get_groes.
select single groes from mara into groes
where matnr = matnr.
endmethod.
method write_result.
write:/ matnr, maktx, groes.
uline.
endmethod.
method get_data.
.
.
.
endmethod.
endclass.
data: o_ref type ref to item.
start-of-selection.
create object o_ref
exporting
matnr = 'A103'.
How change this code to create methode get_data which allows get to data outside of class and put it to table itab which will be used in another part of program? Thanks for help.
Regards,
Joanna
‎2007 Jul 17 8:49 AM
Hi Joanna,
I'd think this would be:
public section.
methods:
[...]
get_data
EXPORTING
e_matkx TYPE maktx
e_groes TYPE groes.
[..]
CLASS item IMPLEMENTATION.
METHOD get_data.
e_maktx = ME->maktx.
e_groes = ME->groes.
ENDMETHOD.
Calling would be something like:
types: begin of ty_mat_data,
maktx type maktx,
groes type groes,
end of ty_mat_data.
data: o_ref type ref to item,
gt_mat_data type standard table of ty_mat_data,
gs_mat_data like line of gt_mat_data.
start-of-selection.
create object o_ref
exporting
matnr = 'A103'.
call method o_ref->get_matkx.
call method o_ref->get_groes.
call method o_ref->get_data
importing
e_maktx = gs_mat_data-maktx
e_groes = gs_mat_data-groes.
append gs_mat_data to gt_mat_data.
Hope that helps
Frank
‎2007 Jul 17 8:49 AM
Hi Joanna,
I'd think this would be:
public section.
methods:
[...]
get_data
EXPORTING
e_matkx TYPE maktx
e_groes TYPE groes.
[..]
CLASS item IMPLEMENTATION.
METHOD get_data.
e_maktx = ME->maktx.
e_groes = ME->groes.
ENDMETHOD.
Calling would be something like:
types: begin of ty_mat_data,
maktx type maktx,
groes type groes,
end of ty_mat_data.
data: o_ref type ref to item,
gt_mat_data type standard table of ty_mat_data,
gs_mat_data like line of gt_mat_data.
start-of-selection.
create object o_ref
exporting
matnr = 'A103'.
call method o_ref->get_matkx.
call method o_ref->get_groes.
call method o_ref->get_data
importing
e_maktx = gs_mat_data-maktx
e_groes = gs_mat_data-groes.
append gs_mat_data to gt_mat_data.
Hope that helps
Frank
‎2007 Jul 17 9:11 AM
Hi Joanna,
Under Public Section of the Class definition declare an iternal table and its corresponding work area.
PUBLIC SECTION.
DATA: t_mat TYPE STANDARD TABLE OF mara,
w_mat TYPE mara.Then, in the Class Implementation section, follow as under: (Example only)
METHOD get_data.
SELECT * FROM mara
INTO TABLE t_mat
WHERE matnr = me->matnr.
ENDMETHOD. "get_dataThen later in the program if you want to use this data, see as under (E.g. only)
START-OF-SELECTION.
CREATE OBJECT o_ref .....
....................
LOOP AT o_ref->t_mat INTO o_ref->w_mat.
WRITE:/ o_ref->w_mat-matnr,
o_ref->w_mat-ersda,
o_ref->w_mat-ernam.
ENDLOOP.Hope this helps.
Don't forget to reward points!
Cheers,
Sougata.
‎2007 Jul 17 9:23 AM
Hi Sougata,
you can do it like that, but -with all respect due- it wouldn't make much sense.
- You don't need an internal table of mara if you have only 1 material number.
- Making this internal table public would be against the OO principles of encapsulation. If so at all it should be "public read-only".
- If you want to pass an internal table in OO you have to define a table type in the dictionary, like ZMARA_T with line type mara or define it in a type pool.
Regards
Frank
‎2007 Jul 17 9:49 AM
Hi Frank,
I had posted the code as an <u>example</u> for an internal table which is visible outside its implemented class; I think that was the original question. I realise its not a correct approach to OO and agree to your first 2 points. The coding approach in the original post was already incorrect and I didn't have time to try and correct the entire coding...instead I made minor changes to show how the internal table data can be accessed outside the implementing class.
On your third point, however, I'm pretty sure that an internal table can be passed in OO without always creating its table type in the ABAP dictionary.
Regards,
Sougata.
‎2007 Jul 17 9:51 AM
‎2007 Jul 17 10:06 AM
@Sougata: Hey, no offense !!! I see your point, but I think Joanna's question had a different background.
@Joanna: you're mostly welcome !