Reference to calling object.

  • Thread starter Thread starter Alan LeHun
  • Start date Start date
A

Alan LeHun

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.
 
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)
 
Alan LeHun said:
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.
 
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top