‎2011 Jun 01 9:06 PM
Is there a way to retrieve the value of a reference variable (into a string)? So not the dereference, but the actual value of the variable itself. For instance, let's say I have three object reference variables. In the debugger I can see their values, which might look like this:
It's these values I want to retrieve in my code. Is this possible?
‎2011 Jun 02 9:40 AM
Hi R,
In C you can create a pointer to the memory of the variable with the &.
In ABAP you have the statement: GET REFERENCE OF <variable> INTO <pointer>.
[http://help.sap.com/saphelp_nw2004s/helpdata/en/4e/fb95f40e8f11d3b9370000e8353423/frameset.htm]
Regards,
Robin Vleeschhouwer
Edited by: R. Vleeschhouwer on Jun 2, 2011 10:40 AM
‎2011 Jun 01 11:02 PM
Hi R.
what you see in the debugger is not the "value" of these reference variables.
is just a representation of the reference. In this case it is indicating this is a pointer to an instanciated class of type ZCL_MYCLASS.
Perhaps the reason you are asking this is that you want to determine at runtime the class type? If so you can use Runtime Type Services ([RTTS|https://wiki.sdn.sap.com/wiki/display/ABAP/RuntimeTypeServices+(RTTS)]) to determine this.
Cheers
Graham Robbo
‎2011 Jun 02 9:03 AM
Thanks Graham, I know. What I tried to explain, is that I want to access the value of the reference variable, not the dereference (in the example, an object of type ZCL_MYCLASS).
If I'm not mistaken, in C it's done by placing the & operator in front of the variable name.
‎2011 Jun 02 9:40 AM
Hi R,
In C you can create a pointer to the memory of the variable with the &.
In ABAP you have the statement: GET REFERENCE OF <variable> INTO <pointer>.
[http://help.sap.com/saphelp_nw2004s/helpdata/en/4e/fb95f40e8f11d3b9370000e8353423/frameset.htm]
Regards,
Robin Vleeschhouwer
Edited by: R. Vleeschhouwer on Jun 2, 2011 10:40 AM
‎2011 Jun 02 9:52 AM
Robin, I tried that too, but that way I'm still only able to access the dereference.
‎2011 Jun 02 10:00 AM
I just noticed the following in the ABAP documentation:
In ABAP, however, there is no real equivalent to pointers in the sense of variables which contain a memory address and can be used without the contents operator. You can only work with the data object to which a field symbol points.
I guess it's not possible then. Unless someone knows a trick to do it anyway?
‎2011 Jun 02 12:44 PM
Hello,
May be this is the trick you're looking for: [Accessing the inaccessible|http://wiki.sdn.sap.com/wiki/display/ABAP/AccessingtheInacessible-LocalClasseswithinGlobalClasses]
BR,
Suhas
PS: BTW did you try out Graham's suggestion to use the RTTI class?
‎2011 Jun 02 5:18 PM
Suhas, I'm not looking for ways to circumvent encapsulation, neither am I interested in runtime type identification. Did you read the entire thread?
‎2011 Jun 03 9:40 AM
> I guess it's not possible then. Unless someone knows a trick to do it anyway?
Yes, I definitely agree. You can't access the address of either data reference or object reference which is "pointing" to a data and object respectively. There is also no trick to overcome this.
I think the reason is quite obvious. C language is low level one, where accessing physical memory is often inevitable. ABAP is a high level language like JAVA where such inner low level details should not be of prime developer's interests (if at all). So the language designers didn't predict there will be a case when businees oriented developer should need to be provided with such information. It's simply quite hard to imagine the case where we could take advantage of that.
Regards
Marcin
‎2011 Jun 03 10:08 AM
Marcin, the reason I asked is because I was looking for a way to (more or less) uniquely identify objects, the way Java does with its default implementation of the hashCode() method on the Object class, without defining a new root class myself.
I figured the only way to do this is to get the reference variable's value (the memory address), but alas, ABAP doesn't allow it.
So now I've come up with a solution using a guid and a custom hash implementation which seems to be working fine.
‎2011 Jun 03 10:51 AM
Yes René, a GUID usage is fine enought to assure uniqueness of the objects.
Regards
Marcin