‎2007 Aug 06 5:24 PM
I just created a new class(A), now I need to create another class that has an array of the class (A). How can I do it?
10nks
Gabriel
‎2007 Aug 06 6:28 PM
This what you are looking for? LCL_B is a class with an attribute which is a table of refereces of LCL_A.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_a DEFINTION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_a definition.
public section.
data: a_attribute type string.
methods: constructor importing im_attr type string.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_a IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_a implementation.
method constructor.
a_attribute = im_attr.
endmethod.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_b DEFINTION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_b definition.
public section.
data: o_list type table of ref to lcl_a.
data: o_list_line like line of o_list.
methods: add_to_list importing im_object type ref to lcl_a,
write_list.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_b DEFINTION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_b implementation..
method add_to_list.
o_list_line = im_object.
append o_list_line to o_list.
endmethod.
method write_list.
loop at o_list into o_list_line.
write:/ o_list_line->a_attribute.
endloop.
endmethod.
endclass.
start-of-selection.
data: a_obj type ref to lcl_a.
data: b_obj type ref to lcl_b.
create object b_obj.
create object a_obj
exporting im_attr = 'This is 1 object'.
call method b_obj->add_to_list( a_obj ).
create object a_obj
exporting im_attr = 'This is 2 object'.
call method b_obj->add_to_list( a_obj ).
create object a_obj
exporting im_attr = 'This is 3 object'.
call method b_obj->add_to_list( a_obj ).
call method b_obj->write_list( ).
Regards,
Rich Heilman
‎2007 Aug 06 6:28 PM
This what you are looking for? LCL_B is a class with an attribute which is a table of refereces of LCL_A.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_a DEFINTION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_a definition.
public section.
data: a_attribute type string.
methods: constructor importing im_attr type string.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_a IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_a implementation.
method constructor.
a_attribute = im_attr.
endmethod.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_b DEFINTION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_b definition.
public section.
data: o_list type table of ref to lcl_a.
data: o_list_line like line of o_list.
methods: add_to_list importing im_object type ref to lcl_a,
write_list.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_b DEFINTION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_b implementation..
method add_to_list.
o_list_line = im_object.
append o_list_line to o_list.
endmethod.
method write_list.
loop at o_list into o_list_line.
write:/ o_list_line->a_attribute.
endloop.
endmethod.
endclass.
start-of-selection.
data: a_obj type ref to lcl_a.
data: b_obj type ref to lcl_b.
create object b_obj.
create object a_obj
exporting im_attr = 'This is 1 object'.
call method b_obj->add_to_list( a_obj ).
create object a_obj
exporting im_attr = 'This is 2 object'.
call method b_obj->add_to_list( a_obj ).
create object a_obj
exporting im_attr = 'This is 3 object'.
call method b_obj->add_to_list( a_obj ).
call method b_obj->write_list( ).
Regards,
Rich Heilman
‎2007 Sep 04 8:55 AM
this is OK for local classes
for repository classes, as each attribute must be typed with some type from the DDIC, you have to create a table type
go to SE11, create a new data type. Popup asks what you want to create: specify "table type". Next you have to provide the row type, there you put class name.
in your class B, create an attribute with type corresponding to your new table type