473,382 Members | 1,420 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,382 software developers and data experts.

Reference to calling object.


Is it possible for a subroutine to get a reference to the object
instance that has called it?

IOW, class1 creates an instance of class2 and calls one if its
subroutines and I would like that subroutine to have a reference to the
original class1 object that called it.

--
Alan LeHun
Nov 21 '05 #1
4 1191
I'd rather see you pass this as a "sender" object, but you can get it
from the Environment

the following code works
Dim strStackTrace As String = Environment.StackTrace
Dim nPositionStart As Integer =
strStackTrace.IndexOf("StdHandle.SomeButton_Click" )
nPositionStart = strStackTrace.IndexOf("at", nPositionStart)
Dim nPositionStop As Integer = strStackTrace.IndexOf("at",
nPositionStart + 2)
strStackTrace = strStackTrace.Substring(nPositionStart + 3,
nPositionStop - nPositionStart - 3)
strStackTrace = strStackTrace.Substring(0, strStackTrace.IndexOf("
in"))
MessageBox.Show(strStackTrace)

Nov 21 '05 #2
replace the "StdHandle.SomeButton_Click" with your Class.Method

Nov 21 '05 #3
"Alan LeHun" <tr*@reply.to> wrote in message
news:MP************************@news.clara.net...
Is it possible for a subroutine to get a reference to the object
instance that has called it?


Of course; pass one in.

[Class1]
Sub Caller
Dim x as New Class2
x.Beep( Me )
End Sub

[Class2]
Public Sub Beep( ByVal oaCaller as Class1 )
Beep()
End Sub

All of which is fine, so long as these two are fairly "close" - once
they start drifting apart, say, into separate assemblies, the Trouble
starts - but we'll leave that for another day ... ;-)

HTH,
Phill W.
Nov 21 '05 #4
In article <d9**********@yarrow.open.ac.uk>, P.A.Ward@o-p-e-n-.-a-c-.-u-
k says...
[Class1]
Sub Caller
Dim x as New Class2
x.Beep( Me )
End Sub

[Class2]
Public Sub Beep( ByVal oaCaller as Class1 )
Beep()
End Sub

All of which is fine, so long as these two are fairly "close" - once
they start drifting apart, say, into separate assemblies, the Trouble
starts - but we'll leave that for another day ... ;-)


That does the job, tyvm. I'm not sure what this means for encapsulation
but I only need it for debugging so that's ok. :)

cheers, and thanks to stand_sure, also.

--
Alan LeHun
Nov 21 '05 #5

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

Similar topics

4
by: Murat Tasan | last post by:
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...
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: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
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...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
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...
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...
9
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
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'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.