Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Retrieving a reference variable value

Former Member
0 Likes
3,864

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?

1 ACCEPTED SOLUTION
Read only

RobinV
Participant
0 Likes
3,006

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

10 REPLIES 10
Read only

GrahamRobbo
SAP Mentor
SAP Mentor
0 Likes
3,006

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

Read only

0 Likes
3,006

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.

Read only

RobinV
Participant
0 Likes
3,007

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

Read only

Former Member
0 Likes
3,006

Robin, I tried that too, but that way I'm still only able to access the dereference.

Read only

Former Member
0 Likes
3,006

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?

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,006

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?

Read only

Former Member
0 Likes
3,006

Suhas, I'm not looking for ways to circumvent encapsulation, neither am I interested in runtime type identification. Did you read the entire thread?

Read only

MarcinPciak
Active Contributor
0 Likes
3,006

> 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

Read only

Former Member
0 Likes
3,006

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.

Read only

MarcinPciak
Active Contributor
0 Likes
3,006

Yes René, a GUID usage is fine enought to assure uniqueness of the objects.

Regards

Marcin