473,574 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.edu
http://genomics.cwru.edu

Jul 17 '05 #1
4 9623
Murat Tasan <ta***@eecs.cwr u.edu> wrote in message news:<Pine.SOL. 4.53.0401121353 170.947@zoidber g>...
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.cwr u.edu> wrote in message
news:Pine.SOL.4 .53.04011213531 70.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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.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 "constructe d" 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.parentObje ct = 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.cwr u.edu> wrote in message
news:Pine.SOL.4 .53.04011213531 70.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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.edu
http://genomics.cwru.edu



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

Jul 17 '05 #4
Murat Tasan <ta***@eecs.cwr u.edu> wrote in message news:<Pine.SOL. 4.53.0401131014 290.1226@zoidbe rg>...
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 classcastexcept ion 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
9569
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. In Opera (7.23/Win), the call appears to do nothing - the node remains - but no errors are shown. In Netscape (7.0/Win), an exception results....
5
2939
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 would be passed "by value". I now believe I was incorrect and that it's only types like int, bool, etc. that behave like that? i.e. things like...
0
2788
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 from button and I want to have a reference to TextBox to make use of it.The most difficult was the reference to TextBox. This sample is taken from...
3
11089
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 the code, to check whether all the Connections are closed. everything is perfect, and infact the connections are closed in the finally block.
8
2392
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
8
1729
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 find any documentation online with regards to it's methods. Also, as I am learning, it would be nice to be able to know what methods are available...
4
1744
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 the COM object will never get release, Even we call gc.Collect() But the same code compiles in .NET 2.0 works without the...
1
2013
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 method that will start another automation server. These Activex automation servers that it talks to require a specific interface that the calling...
275
12157
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
7815
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7828
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6476
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5316
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3765
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2253
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.