‎2009 Jun 30 7:50 AM
Hello everyone,
I've a strange thought here, what is a data object which point to an object. Below is the example coding.
after creating the data object REF_ANY, what exactly it is?
CLASS MAN DEFINITION .
PUBLIC SECTION.
DATA AGE TYPE I.
METHODS: CONSTRUCTOR.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS MAN IMPLEMENTATION.
METHOD CONSTRUCTOR.
AGE = 25.
ENDMETHOD.
ENDCLASS.
DATA REF_ANY TYPE REF TO DATA.
FIELD-SYMBOLS: <FS_ANY> TYPE ANY.
START-OF-SELECTION.
CREATE DATA REF_ANY TYPE REF TO MAN.
ASSIGN REF_ANY->* TO <FS_ANY>.
WRITE: 'KKK'.
‎2009 Jun 30 8:27 AM
Hi,
First of all, we distinguish reference to data so called data reference and reference to object so called object reference
For that we use data reference/object variable respectively. I.e.
data: ref_data type ref to data. "data reference variable - so variable which can hold address of data in memory
"conversely
data: ref_object type ref to object. "object reference variable - so variable which can hold address of object in memory
Generally all references are nothing but pointers . This means that when working with such data you don't know its name, but you hold its address in memory. This way it allows to deference it
assign ref->* to ...
in order to get is value. Please note, that this is only applicable for data references not for object reference . You can't get value of and object, it would not make sense. So this is incorrect (for objects).
ASSIGN REF_ANY->* TO <FS_ANY>.
Objects can only be address throught pointers (throught their phisicall address in memory), so your code is incorrect. Moreover you need to create refrence to an object not data so CREATE OBJECT must be used. Look at this code:
"this is generic reference variable (which means can hold any object's address)
data: ref_object type ref to object. "OBJECT is root node where all the objects inherit from (similar to TYPE REF TO DATA for data)
"now as we have reference variable which can hold address of any object we create an object which is typed as our class MAN
CREATE OBJECT ref_object TYPE man.
"what I do above is creating an object of class MAN (type man) and store its address in REF_OBJECT
"if you write something like
CREATE OBJECT ref_object. "it would be incorrect as you don't know real type (it must be said explicitly either during declaration or during creation)
The simpler altenrative to above would be
data: ref_man type ref to man. "declare reference variable typed of class MAN
"now create the object
CREATE OBJECT ref_man. "here the statement is correct as we alrerady know the type
I know it may seem to be a little bit mixed. In order to get more information please refer
[Data references|http://help.sap.com/saphelp_nw70/helpdata/en/14/11e70b0c5c11d3b9350000e8353423/frameset.htm]
[Object references|http://help.sap.com/saphelp_nw70/helpdata/EN/c3/225b5f54f411d194a60000e8353423/frameset.htm]
Regards
Marcin
‎2009 Jun 30 8:03 AM
hai
data is a generi type and u can assign any oobject to it
CREATE DATA REF_ANY TYPE REF TO MAN.
thsi statement creates an object ref_any which is type of class MAN
ASSIGN REF_ANY->* TO <FS_ANY>.
now ref_any is a class which poitn sto some class so u want to assign it to soem field symbol for further process
so u use referencign operator ->* , this will assign this object to <fs_any>
m.a
‎2009 Jun 30 8:27 AM
Hi,
First of all, we distinguish reference to data so called data reference and reference to object so called object reference
For that we use data reference/object variable respectively. I.e.
data: ref_data type ref to data. "data reference variable - so variable which can hold address of data in memory
"conversely
data: ref_object type ref to object. "object reference variable - so variable which can hold address of object in memory
Generally all references are nothing but pointers . This means that when working with such data you don't know its name, but you hold its address in memory. This way it allows to deference it
assign ref->* to ...
in order to get is value. Please note, that this is only applicable for data references not for object reference . You can't get value of and object, it would not make sense. So this is incorrect (for objects).
ASSIGN REF_ANY->* TO <FS_ANY>.
Objects can only be address throught pointers (throught their phisicall address in memory), so your code is incorrect. Moreover you need to create refrence to an object not data so CREATE OBJECT must be used. Look at this code:
"this is generic reference variable (which means can hold any object's address)
data: ref_object type ref to object. "OBJECT is root node where all the objects inherit from (similar to TYPE REF TO DATA for data)
"now as we have reference variable which can hold address of any object we create an object which is typed as our class MAN
CREATE OBJECT ref_object TYPE man.
"what I do above is creating an object of class MAN (type man) and store its address in REF_OBJECT
"if you write something like
CREATE OBJECT ref_object. "it would be incorrect as you don't know real type (it must be said explicitly either during declaration or during creation)
The simpler altenrative to above would be
data: ref_man type ref to man. "declare reference variable typed of class MAN
"now create the object
CREATE OBJECT ref_man. "here the statement is correct as we alrerady know the type
I know it may seem to be a little bit mixed. In order to get more information please refer
[Data references|http://help.sap.com/saphelp_nw70/helpdata/en/14/11e70b0c5c11d3b9350000e8353423/frameset.htm]
[Object references|http://help.sap.com/saphelp_nw70/helpdata/EN/c3/225b5f54f411d194a60000e8353423/frameset.htm]
Regards
Marcin
‎2009 Jun 30 8:44 AM
Hi Marcin,
Thanks for your detailed answer. It is much more clear.
But abap workbench can't find out syntax error in my coding, it's so strange.
‎2009 Jun 30 8:51 AM
Yup, it will not find the syntax error as syntax is correct but not the logic. Only during runtime system tries to create reference
CREATE DATA REF_ANY TYPE REF TO MAN.
but it recognizes it as data reference so it will be pointing to nothing.
Once you change the declaration to
data: ref_any type ref to object.
you will get two syntax errors - first that you are trying to tie a reference to data (CREATE DATA) and should be (CREATE OBJECT), next that you want to dereference (get value of) object - which is impossible.
Regards
Marcin
‎2009 Jun 30 8:52 AM
Hi Marcin,
I've another question here, could you please help me out?
what's the difference between
assign ref_data to <fs1>.
and
assign ref_data->* to <fs2>?
data ref_data type REF TO data.
FIELD-SYMBOLS: <fs1> type any,
<fs2> type any.
START-OF-SELECTION.
CREATE DATA ref_data type i.
ASSIGN ref_data to <fs1>.
ASSIGN ref_data->* to <fs2>.
Thanks and Regards,
Aaron
‎2009 Jun 30 9:09 AM
ASSIGN some_data to <fs1>. is applicable only for data which don't hold any address but are statically known i.e
field-symobls: <fs> type any.
data: my_var type i. "statically known data MY_VAR
ASSIGN my_var TO <FS>. "now <FS> has excatly the same meaning as you would use MY_VAR
<fs> = 5.
write: 'Field symbol holds value', <fs>.
write: 'MY_VAR holds the same value', my_var.
On the other hand, we use ASSIGN ref_data->* to <fs1> only for references (data which can hold addresses)
field-symobls: <fs> type any.
data: my_ref type ref to data.
data: my_var type i.
GET REFERENCE OF my_var INTO my_ref. "MY_REF points to (store address of) MY_VAR
"here we must dereference this address (get value which is hidden beyond this address)
"for this we use
ASSIGN my_ref->* to <fs1>.
"now we do the same as above
<fs> = 5.
write: 'Field symbol holds value', <fs>.
write: 'MY_VAR holds the same value', my_var.
Generally field symbols are used for addressing components (but through its VALUE ), while references to strore data/object real ADDRESS . In order to get the value (or assing a value to) this varibale which is beyond this address you have to combine field symbols with data references (as in second example)
One note!
Next time please create another thread if the original question was answered and you want to get more information. It will not only allow to appreciate people for good answers, but will also let the others contribute to the question.
Regards
Marcin
‎2009 Jun 30 9:29 AM
Hi Marcin,
Thanks for you help indeed. And I'll create a new thread next time.
Thanks and Regards,
Aaron.