473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

obtaining a reference to the calling object

i have a quick question... is there a way to obtain the reference to the
object which called the currently executing method?

here is the scenario, i have a class and a field which i would like to
populate with a reference to the object that constructed this current
object. i would attempt to accomplish this by setting the appropriate
field from within the constructor...

i figured it might be obtainable from the stack trace, but that doesn't
seem to work.

oh, and i do know that i could just add an extra parameter to the
constructor and from the calling method just pass "this", but i don't want
to have to trust that the people using my code will actually do this
correctly each time.

thanks for any input,

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
4 9605
Murat Tasan <ta***@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0401121353170.947@zoidberg>...
i have a quick question... is there a way to obtain the reference to the
object which called the currently executing method?

here is the scenario, i have a class and a field which i would like to
populate with a reference to the object that constructed this current
object. i would attempt to accomplish this by setting the appropriate
field from within the constructor...

i figured it might be obtainable from the stack trace, but that doesn't
seem to work.


What is the unworking code?

Greg
Jul 17 '05 #2
nos

"Murat Tasan" <ta***@eecs.cwru.edu> wrote in message
news:Pine.SOL.4.53.0401121353170.947@zoidberg...
i have a quick question... is there a way to obtain the reference to the
object which called the currently executing method?

here is the scenario, i have a class and a field which i would like to
populate with a reference to the object that constructed this current
object. i would attempt to accomplish this by setting the appropriate
field from within the constructor...
Constructed or called?

i figured it might be obtainable from the stack trace, but that doesn't
seem to work.

oh, and i do know that i could just add an extra parameter to the
constructor and from the calling method just pass "this", but i don't want
to have to trust that the people using my code will actually do this
correctly each time.

thanks for any input,

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #3
really either. if "called", the called method would be a factory method,
which would at some point set the appropriate calling object parameter...
and if "constructed" the constructor would include nearly the same line.

so basically, if in a factory "called" method which has been called by a
method in object x, a new object y is created and the correct parameter in
y is set through something like this:

y.parentObject = x

(but without passing x (as a parameter set to value "this") from x, i
don't know how to get a reference to x)

if done in a constructor for y:

this.parentObject = x

but same problem again.

the reason i don't want to have people pass the parent object as a
parameter is it opens the door for incorrect objects to be passed... that
is let us assume that the constructor option is used and the constructor
includes a parameter for identifying the parent object... so from within x
there is a line in a method like so:

yClass y = new yClass(this);

that would work, but someone could also then do this:

yClass y = new yClass(z);

where z is some other object, possibly of the same class as x (so checking
the class of the object isn't enough to catch the problem).

i want to guarantee that the parentObject reference in y correctly points
to x by doing it automatically.

thanks again,

murat

On Mon, 12 Jan 2004, nos wrote:

"Murat Tasan" <ta***@eecs.cwru.edu> wrote in message
news:Pine.SOL.4.53.0401121353170.947@zoidberg...
i have a quick question... is there a way to obtain the reference to the
object which called the currently executing method?

here is the scenario, i have a class and a field which i would like to
populate with a reference to the object that constructed this current
object. i would attempt to accomplish this by setting the appropriate
field from within the constructor...


Constructed or called?

i figured it might be obtainable from the stack trace, but that doesn't
seem to work.

oh, and i do know that i could just add an extra parameter to the
constructor and from the calling method just pass "this", but i don't want
to have to trust that the people using my code will actually do this
correctly each time.

thanks for any input,

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu



--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #4
Murat Tasan <ta***@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0401131014290.1226@zoidberg>.. .
but same problem again.

the reason i don't want to have people pass the parent object as a
parameter is it opens the door for incorrect objects to be passed... that
is let us assume that the constructor option is used and the constructor
includes a parameter for identifying the parent object... so from within x
there is a line in a method like so:


You could add an extra parameter, and then add some assertions
ensuring the object is the desired instanceof.

....But this raises a bigger question. There should be no reason why
you would ever want to do this. If there are a number of possible
objects that a static method or other object can operate on, these
objects should share a common parent object or interface. Then you can
specify the interface and a classcastexception will result if abused.
Otherwise, it should produce some non-error result for every object
(like toString())

Rethink the design and odds are you can avoid this API. What you want
to do may be the only good option, but it sounds like a big hack, and
your maintenence concerns are wellfounded.

---
Jared Dykstra
http://www.bork.org/~jared
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Michael Winter | last post by:
In an attempt to answer another question in this group, I've had to resort to calling the DOM method, Node.removeChild(), using a reference to it (long story...). That is, passing Node.removeChild....
5
by: Jerry Morton | last post by:
Hi, I'm new to C# and have run into a problem of my own making! I originally took from the documentation that as long as I didn't use the "ref" keyword in method declarations, that everything...
0
by: Andrea Trevisan | last post by:
That's a revival of a known thing I suppose.I hope it's useful. My problem was: I want to have a DataGrid with two Template columns: first with TextBox,second with Button.I want to fire an event...
3
by: Kamalanathan T. | last post by:
Hi, We have developed an Web application in ASP.NET with C# and we r using SQL Server 2000. We get the Timeout expired error, when more than 300 concurrent users hit the site. I hagone thru...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
8
by: Daz | last post by:
Hi everyone. I just wanted to know if there was any way to use script to display a list of methods linked to a particular object? The object I have in mind is getBrowser(), and I can't seem to...
4
by: johnxhc | last post by:
We have a project in .NET 1.1 , some of the .NET methods take COM interface reference pointer as a parameter, somehow we have to call Marshal.ReleaseComObject to release the COM Object, otherwise...
1
by: Mike H | last post by:
I can't seem to figure out the right thing to search on, so I thought I'd just provide an example and see if someone can point me to some tips. What I have is an Automation Server that has a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.