Results 1 to 3 of 3

Thread: Reference to caller object

  1. #1

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66

    Reference to caller object

    Is it possible to get a reference to the object that is calling a function inside that function? (Without passing it has a parameter)

    Example:
    Code:
    Interface I
    {
         void CallerMethod();
    
         void CalledBackMethod();
    }
    
    public class A : I
    {
         public void CallerMethod()
         {
              B b = new B();
              b.CalledMethod();
         }
    
         public void CalledBackMethod()
         {
              //Do something
         }
    }
    
    public class B
    {
         public void CalledMethod()
         {
              I i = GetCallingObject();  //This is what I want!!
              i.CalledBackMethod()
         }
    }
    Or is there other ways of doing this?

    Thanks!

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I don't think you can. For a example, look at all the build in methods that handle events, the signatures all have a sender object passed in as an argument. It would seem to me that MS would avoided it if they could do it another way... But I have no proof one way or the other.

  3. #3

    Thread Starter
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    Thanks hellswraith. I had myself noticed that adopted behaviour by Microsoft. It seems, like you say, I have to do the same...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width