Access Data types or class definition of other programs in your program
HI Guys,
For past few days I was struggling hard to figure out whether we can use data types or class definitions of other programs in our programs or enhancements without using statement include or copy paste entire code in our source code. At last I have found SAP standard program which answers all my questions.
‘DEMO_CREATE_REFERENCE’ is the program which illustrates this. Here is the sample code.
DATA: CLS_NAME TYPE STRING,
REF TYPE REF TO DATA.
CLS_NAME = ‘PROGRAM=DEMO_CREATE_REFERENCECLASS=CLS’.
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
->with the above statement REF will have type CLASS-CLS of program DEMO_CREATE_REFERENCE.
For eg-
CLS_NAME = ‘FUNCTION-POOL=MEREQCLASS=LCL_REQ_ITEM’
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
->here REF will have type class – LCL_REQ_ITEM of Function group – MEREQ.
I know you guys might wonder how this stuff is put in use or will help. I shall explain my case where I have come across a situation where I need to define a variable with the type of a class used in other program.
MY SITUATION AND SOLUTION I HAVE FOUND-
Somehow with my requirement I have ended up in class CL_TABSTRIP_VIEW_MM with a need to access an attribute ‘ITEM’ of class LCL_REQ_ITEM_STATE which is defined in Function group MEREQ.Though I can see all the required attributes in debug mode, I was unable to fetch them in the source code, as classes are defined in include of function group not inSE24.At last I have found myself a solution, below is the approach and situtaion.
DATA: CLS_NAME TYPE STRING,
REF TYPE REF TO DATA,
LC_MODEL TYPE REF TO IF_MODEL_HOLDER_MM,
LC_GET_MODEL TYPE REF TO IF_MODEL_MM.
FIELD-SYMBOLS:
TYPE ANY,
TYPE ANY,
TYPE MEREQ_ITEM.
**–Here Subviews is defined parameters in method PBO of class CL_TABSTRIP_VIEW_MM
READ TABLE SUBVIEWS INTO lS_SUBVIEW INDEX 16.
IF SY-SUBRC = 0.
LC_MODEL ?= lS_SUBVIEW.
IF LC_MODEL IS BOUND.
LC_GET_MODEL = LC_MODEL->GET_MODEL( ).
**–Here LC_GET_MODEL will have data ref to type class LCL_REQ_ITEM of function pool-MEREQ, So, here is the need to **define a varaible with type ref to a class defined in another program/function pool.
CLS_NAME = ‘FUNCTION-POOL=MEREQCLASS=LCL_REQ_ITEM’
CREATE OBJECT REF TYPE REF TO (CLS_NAME).
ASSIGN REF->* TO
**–Now, Assign Model to its type and access required attributes
?= LC_GET_MODEL
IF IS ASSIGNED.
ASSIGN (‘-MY_STATE’) TO .
IF IS ASSIGNED.
ASSIGN (‘-ITEM’) TO .
ENDIF. “IF IS ASSIGNED.
ENDIF. “IF IS ASSIGNED.
ENDIF. “IF LC_MODEL IS BOUND.
ENDIF. “READ TABLE SUBVIEWS INTO lS_SUBVIEW INDEX 16.
will have the required information which we are looking for.
New NetWeaver Information at SAP.com
Very Helpfull