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

ABAP Object - beginner problems

Former Member
0 Likes
630

Hi all,

i got a little problem with understanding the logic of ABAP Objects. For example the object material. The material has a number, text, size and so on. That's no problem i can define a class with the attributes number, text size and so on and I can create an object. But how do I interact with more than one object. I want to read 100 materials from the database and after that i want to list them in an alv grid. Do I have to create hundred objects?

REPORT Test.

CLASS material DEFINITION.

PUBLIC SECTION.

DATA:

number TYPE i,

text(20) TYPE c,

size TYPE c.

ENDCLASS.

CLASS material IMPLEMENTATION.

ENDCLASS.

DATA:

obj_material TYPE REF TO material.

START-OF-SELECTION.

????????

CREATE OBJCET obj_material.

SELECT * FROM mara INTO obj_material??????

Don't know how to....

Thanks

Matthias Tripp

4 REPLIES 4
Read only

uwe_schieferstein
Active Contributor
0 Likes
540

Hello Matthias

I am not sure if there is already a standard class available for materials. For purchase orders, for example, there is a standard class available (search for CLPOMM in SE24).

Now back to your question. If you want to create hundreds of material object instances then you have create the same number of instances. I will give you another example. Class CF_REBD_BUILDING (RE-FX) has a method FIND which returns the instance of a building if it exists. Now if you you need a list of buildings then you have to loop over this list and create the instances, e.g.:

DATA:
    lo_building   TYPE REF TO if_rebd_building,
    lt_buildings  TYPE TABLE OF if_rebd_building.  " not sure if it possible like this
  
  LOOP AT gt_itab INTO ls_entry.
    CLEAR: lo_building.

    lo_building = cf_rebd_building=>find(... ).
    IF ( lo_building IS BOUND ).
      APPEND lo_building TO lt_buildings.
    ENDIF.
  ENDLOOP.

Regards

Uwe

ENDLOOP.

Read only

Former Member
0 Likes
540

hi,

there is a concept called POINTER TABLES in Abap objects.

check the below ex:

in a loop i am trying to create the same object many times and appending it to another table(table of objects).

you can apply this to ur program.

write a select-endselect in the method implementation fetching the values into your variables, and create the object in loop and append it.call it later as shown below.

class testclass definition.

public section.

methods : testmethod .

class-data : num type i.

endclass.

class testclass implementation.

method : testmethod.

num = num + 5.

write:/5 num.

endmethod.

endclass.

start-of-selection.

data : myobj type ref to testclass ,

myobj_tab type table of ref to testclass.

<b>do 5 times.

create object myobj .

append myobj to myobj_tab.

enddo.</b>

loop at myobj_tab into myobj.

call method : myobj->testmethod.

endloop.

Regards

Read only

Former Member
0 Likes
540

Thank you....

Read only

thomasalexander_ritter
Product and Topic Expert
Product and Topic Expert
0 Likes
540

Hi,

I just want to add that creating objects is expensive performance wise. So you should always aim to create only objects which are absolutely necessary. So for example if a search returns 100 hits but the alv grid can show only 10 at once then try to create only 10 objects.

cheers

Thomas