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

Array Object

Former Member
0 Likes
490

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

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
421

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

2 REPLIES 2
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
422

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

Read only

0 Likes
421

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