Calling Object

  • Thread starter Thread starter MAF
  • Start date Start date
M

MAF

Is there anyway that I can find out what object called another object?

ObjectABase
ObjectA inherits from ObjectABase

objectA calls objectB

I want to invoke a method in ObjectABase?
 
If you have the ability to modify the definition of objectB, you can change
objectB's method to take a parameter of type ObjectABase. Then objectA can
pass itself as a parameter when it calls objectB's method, thereby allowing
objectB to know which objectA subclass called its method.

You may want to reconsider the design of your objects. If Object A is
dependent on Object B and Object B is dependent on Object A, it might mean
that you want to refactor the objects so that they are less dependent on one
another. This may involve moving methods around so that the objects are
more independent or merging both of the objects into the same class if it's
not too large.

Often, the problem you are having comes up in Multiple Dispatch (a situation
where the function to be executed depends on the types of objects passed to
the function). C# has many ways of determining run time type information.
If you are trying to solve a multiple dispatch problem, take a look at the
typeof statement and the GetType method of objects. You may also want to
look up the Visitor Design Pattern, which show's one solution to the double
dispatch problem.

good luck,
-keen
 
Back
Top