How to know the Calling Object ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
vsr said:
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?
 
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?
 
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???
 
vsr said:
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.
 
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.
 
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.
 
vsr said:
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.
 
Back
Top