2007 Nov 12 11:42 AM
hi all ,
i need to check wheather instance for a particular class is already created or not .
i will be passing the class name and i need the result.
Thanks and regards.
JK
2007 Nov 12 2:10 PM
Hi Jayakumar,
I've alread answered this question on SDN Forum, you can see here que this .
You need control this in the class as the follow example.
*----------------------------------------------------------------------*
* CLASS one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS create_objects EXPORTING obj_ref TYPE REF TO one.
CLASS-METHODS clear_objects.
PROTECTED SECTION.
PRIVATE SECTION.
CLASS-DATA obj_one TYPE REF TO one.
ENDCLASS. "one DEFINITION
*----------------------------------------------------------------------*
* CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
METHOD create_objects.
IF obj_one IS INITIAL.
" Create a new instance
CREATE OBJECT obj_one.
MOVE obj_one TO obj_ref.
ELSE.
" Use an existing instance
MOVE obj_one TO obj_ref.
ENDIF.
ENDMETHOD. "create_objects
METHOD clear_objects.
CLEAR obj_one.
ENDMETHOD. "clear_objects
ENDCLASS. "one IMPLEMENTATION
DATA: obj1 TYPE REF TO one,
obj2 TYPE REF TO one.
START-OF-SELECTION.
" Now you'll create the first instance for your class
CALL METHOD one=>create_objects
IMPORTING
obj_ref = obj1.
" Now you'll use an existing instance
CALL METHOD one=>create_objects
IMPORTING
obj_ref = obj2.
CLEAR obj1.
one=>clear_objects( ).
" Now you'll be able to create a new instance for your class
CALL METHOD one=>create_objects
IMPORTING
obj_ref = obj2.
I Hope it's usefull for you !
Regards.
Marcelo Ramos
hi all ,
i need to check wheather instance for a particular class is already created or not .
i will be passing the class name and i need the result.
Thanks and regards.
JK
2007 Nov 12 12:06 PM
Hello Jayakumar
I assume you want to check whether your reference variable hold an instance already. There are two ways available:
DATA:
lo_myref TYPE REF TO <class name / interface name>.
IF ( lo_myref IS INITIAL ).
ELSE.
" we have an instance
ENDIF.
" Since SAP release >= 6.20 a more specific statement is available:
IF ( lo_myref IS BOUND ).
" we have an instance
ELSE.
ENDIF.Regards
Uwe
2007 Nov 12 1:28 PM
no its not like this ..
i have a constructor in the class.
so when i create instance for this class the constructor is fired .
in the constructor i need to check weather an instance has been already created or not .
if its created i will take the reference from that
if its not created then i will allow to create new instance.
2007 Nov 12 2:10 PM
Hi Jayakumar,
I've alread answered this question on SDN Forum, you can see here que this .
You need control this in the class as the follow example.
*----------------------------------------------------------------------*
* CLASS one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS create_objects EXPORTING obj_ref TYPE REF TO one.
CLASS-METHODS clear_objects.
PROTECTED SECTION.
PRIVATE SECTION.
CLASS-DATA obj_one TYPE REF TO one.
ENDCLASS. "one DEFINITION
*----------------------------------------------------------------------*
* CLASS one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
METHOD create_objects.
IF obj_one IS INITIAL.
" Create a new instance
CREATE OBJECT obj_one.
MOVE obj_one TO obj_ref.
ELSE.
" Use an existing instance
MOVE obj_one TO obj_ref.
ENDIF.
ENDMETHOD. "create_objects
METHOD clear_objects.
CLEAR obj_one.
ENDMETHOD. "clear_objects
ENDCLASS. "one IMPLEMENTATION
DATA: obj1 TYPE REF TO one,
obj2 TYPE REF TO one.
START-OF-SELECTION.
" Now you'll create the first instance for your class
CALL METHOD one=>create_objects
IMPORTING
obj_ref = obj1.
" Now you'll use an existing instance
CALL METHOD one=>create_objects
IMPORTING
obj_ref = obj2.
CLEAR obj1.
one=>clear_objects( ).
" Now you'll be able to create a new instance for your class
CALL METHOD one=>create_objects
IMPORTING
obj_ref = obj2.
I Hope it's usefull for you !
Regards.
Marcelo Ramos
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |