By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

How to know the Calling Object ?

P: n/a
vsr
I am calling one Method in Common class from different classes and i want to
know from which object the call is coming from.. is there any way?
Mar 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"vsr" <vs*@discussions.microsoft.com> schrieb:
I am calling one Method in Common class from different classes and i want
to
know from which object the call is coming from.. is there any way?


No, because this wouldn't make sense in any scenario I can think of. Why
exactly would you need this information?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 29 '06 #2

P: n/a
There is a way using the stacktrace and stackframe, but I agree with
Herfried that there are few if any situations in which it would be
useful / the goal could not be achieved in some other way.

What are you trying to achieve?

Alan?

Mar 29 '06 #3

P: n/a
vsr
May be you are right , this may not be the approach.
I created some classes (these classes are not visible) , and created one
Routing Class.
I want to call those classes through the Routing Class......
Ex:-
Class1,Class2,Class3 (all these classes will have same methods with
different implementation)
RoutingClass (this has GetObject method)

RoutingClass.GetObject should return appropriate Object based on the Object

Is it meaning full???

"AlanT" wrote:
There is a way using the stacktrace and stackframe, but I agree with
Herfried that there are few if any situations in which it would be
useful / the goal could not be achieved in some other way.

What are you trying to achieve?

Alan?

Mar 30 '06 #4

P: n/a

"vsr" <vs*@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
I am calling one Method in Common class from different classes and i want
to
know from which object the call is coming from.. is there any way?


A [properly encapsulated] method really shouldn't care who called it.

If your method /really/ needs to know who called it, pass [a reference to]
the caller as an argument to the method.
Or have different callers use different methods.

Regards,
Phill W.
Mar 30 '06 #5

P: n/a
>From the description it looks like you can just use an interface or
common base class to give each of Class1, Class2, Class3 a common
'Interface' (same methods, different implementation) but I am unsure
about what the Routing class is intended to do

What does 'return the appropriate Object(1) based on the Object(2)'
mean?

It looks like Object(1) is one of class1, class2, class3 et al. but
what is Object(2)?

Alan.

Mar 30 '06 #6

P: n/a
vsr
You are right , we could use Interfaces in place of this , i am trying to go
without interfaces.. anyway thank you for the info , now i think i can design
to accomodate interfaces..

Object2 is the which is trying to call Object1.sorry for the confusion.

"AlanT" wrote:
From the description it looks like you can just use an interface or

common base class to give each of Class1, Class2, Class3 a common
'Interface' (same methods, different implementation) but I am unsure
about what the Routing class is intended to do

What does 'return the appropriate Object(1) based on the Object(2)'
mean?

It looks like Object(1) is one of class1, class2, class3 et al. but
what is Object(2)?

Alan.

Mar 30 '06 #7

P: n/a
> "vsr" <vs*@discussions.microsoft.com> schrieb:
I am calling one Method in Common class from different classes and i
want
to
know from which object the call is coming from.. is there any way?

No, because this wouldn't make sense in any scenario I can think of.
Why exactly would you need this information?


I agree that in most cases, you would break encapsulation by doing this.
Since you did not mention what your goal was, it is hard to know if there
wouldn't be a way of achieving the result. It is possible in 2005 using the
Diagnostics.StackTrace.GetFrame method as follows:

Dim propertyName As String = _
New System.Diagnostics.StackTrace(). _
GetFrame(1).GetMethod.Name

That being said, it is possible that the functionality you wish to achieve
could be better accomplished using Aspects. You may want to read up on Aspect
Oriented Programming a bit to see if it could accomplish what you wish to do.
Mar 30 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.