2006 Mar 14 6:07 AM
I am facing a situation in which i am not able to use an internal table passed by value to a method of a class. I prefer to use pass-by-value in the constructor when i create the class , but I am not able to store the table in the class object for it to be accessed by other methods of the class. If i create a DATA reference it becomes null after the contructor method ends as the variable gets destroyed. Pls help. Thank you.
I am facing a situation in which i am not able to use an internal table passed by value to a method of a class. I prefer to use pass-by-value in the constructor when i create the class , but I am not able to store the table in the class object for it to be accessed by other methods of the class. If i create a DATA reference it becomes null after the contructor method ends as the variable gets destroyed. Pls help. Thank you.
2006 Mar 14 6:23 AM
HI anand
can you post the code
<b>try passing the contents of the internal table.</b>
regards
kishore
Message was edited by: Harikishore Sreenivasulu
2006 Mar 14 6:36 AM
Hi Hari,
This is the code in the constructor of the class which "CHANGES" parameter "it_table TYPE ANY TABLE" passed as a value.
CREATE DATA p_recordset LIKE it_table.
GET REFERENCE OF it_table INTO p_recordset.
p_recordset is of "TYPE REF TO DATA".
The use of p_recordset->* works fine in the constructor method. But becomes null in any other method in the class.
The problem does not occur when i pass p_recordset as a reference. I understand the reason why its null but i want the "it_table" to be passed as value only. Is there any alternative??
2006 Mar 14 6:41 AM
the variable p_recordset has to be declared in the attributes of the class as instance and public attribute
Regards
Raja
2006 Mar 14 6:44 AM
Hi Durairaj,
It is not allowed to declare a class attribute of "TYPE ANY TABLE".. If that could be done i wouldn't have had any problem at all...
2006 Mar 14 6:47 AM
2006 Mar 14 6:51 AM
Hi ramakrishna,
Even that would not help cuz attributes can be declared TYPE ANY TABLE be it instance or static..
Further, the purpose of this global class doesnot allow the field p_recordset to be made static..
2006 Mar 14 6:53 AM
i didnt mean type any.
attribute should be instance, public, type ref to data
Regards
Raja
2006 Mar 14 8:10 AM
Hi ,
Dont pass the table as pass by value or pass by refrence to constructor.
if you decleare the table in the public section of the class the constructor will automatically recognize the internal table.
please refer the below code.
class lcl_class1 definition.
Public section.
*tables mara.
types: begin of ty_itab,
matnr type mara-matnr,
end of ty_itab,
ty_i_itab type standard table of ty_itab.
data : itab type standard table of ty_itab.
methods constructor.
methods display_matnr.
endclass.
class lcl_class1 implementation.
method constructor.
select matnr from mara into table itab.
endmethod.
method display_matnr.
data : l_itab type ty_itab.
loop at itab into l_itab.
write / l_itab-matnr.
endloop.
endmethod.
endclass.
data : o_class1 type ref to lcl_class1.
start-of-selection.
create object o_class1.
call method o_class1->display_matnr.
-
I think this solves your problem.If the problem still remains,Please send me the code.
regards
Anil Kumar K
2006 Mar 14 8:14 AM
Hi anil,
I want to use it in the global class. Its not related to a dictionary table. I want to define a type and pass an internal table of that type..
2006 Mar 14 9:45 AM
Hi,
Look at this program.
REPORT zaustin_calsstest1.
*---------------------------------------------------------------------*
* CLASS C_COUNTER DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS c_counter DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING value(tk) TYPE REF TO data,
print_contents.
PRIVATE SECTION.
DATA count TYPE i.
DATA itabk TYPE REF TO data.
ENDCLASS. "C_COUNTER DEFINITION
*---------------------------------------------------------------------*
* CLASS C_COUNTER IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS c_counter IMPLEMENTATION.
METHOD constructor.
itabk = tk.
ENDMETHOD. "CONSTRUCTOR
METHOD print_contents.
FIELD-SYMBOLS : <fs> TYPE ANY TABLE.
FIELD-SYMBOLS : <fc> TYPE line.
FIELD-SYMBOLS : <fk> TYPE ANY.
DATA retval LIKE sy-subrc VALUE '0'.
ASSIGN itabk->* TO <fs>.
LOOP AT <fs> ASSIGNING <fc>.
retval = '0'.
WHILE retval = '0'.
ASSIGN COMPONENT sy-index OF STRUCTURE <fc> TO <fk>.
retval = sy-subrc.
WRITE <fk>.
ENDWHILE.
SKIP.
ENDLOOP.
ENDMETHOD. "print_contents
ENDCLASS. "C_COUNTER IMPLEMENTATION
*---------------------------------------------------------------------*
DATA count TYPE REF TO c_counter.
DATA count_value TYPE i.
DATA itab LIKE vbak OCCURS 0.
DATA ktab TYPE REF TO data.
START-OF-SELECTION.
SELECT * INTO TABLE itab FROM vbak UP TO 5 ROWS.
GET REFERENCE OF itab[] INTO ktab.
CREATE OBJECT count EXPORTING tk = ktab .
CALL METHOD count->print_contents.Hope this solves your problem.
regards
austin
ps : reward points if usefull
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |