views:

140

answers:

3

I have three classes like this.

class A
{
    public class innerB
       {
       //Do something
       }

    public class innerC
       {
        //trying to access objB here directly or indirectly over here. 
        //I dont have to create an object of innerB, but to access the object created by A
        //i.e.
             innerB objInnerB = objB;
        //not like this
             innerB objInnerB= new innerB();
       }

private innerB objB{get;set;}  **//Private**

public A()
   {
    objB= new innerB();
   }
}

I want to access the object of class B in Class C that is created by class A. Is it possible somehow to make changes on object of Class A in Class C. Can i get Class A's object by creating event or anyhow.

Edit: My mistake in asking the question above. Object of B created in A is private not public

IS IT POSSIBLE TO DO THIS BY CREATING EVENT

If anyhow I become able to raise an event that can be handled by Class A, then my problem can be solved.

A: 

Since your class B is within the same scope as class C, that is, within class A, you will be able to instantiate the nested type B from nested type C and use it.

Will Marcouiller
I don't think that's what he's looking for - if I'm reading him right he wants to access the objB property of class A within innerC WITHOUT passing it along.
Andrew Anderson
+1  A: 

You need to pass a reference of objB to innerC, perhaps in the constructor, like: public class innerC { innerB refB; public innerC(innerB objB){ refB = objB; } } Then you can use that reference in all of your innerC methods.

Daniel Rasmussen
@Cyclotis: I am trying to do this at an event that was raised by some user control whose handle has been defined in class B having collection of Class C's objects.
Shantanu Gupta
Could your event arguments hold a reference to the object you need?
Daniel Rasmussen
@Cyclotis: I don't think so, as that event is being raised by a user control which again was binded by Collection that is being holded by Class B containing the collection of Class C. I know this is bit confusing at this moment.
Shantanu Gupta
@Ctclotis: Is it somehow possible to raise an event there itself which can be handled by class A. I can bind any event at A
Shantanu Gupta
Maybe it would be best to explain what you're trying to do, instead of just how you're trying to get there. I'm having a problem understanding to what end you're doing all this...
Daniel Rasmussen
+2  A: 

If I'm reading you correctly you want to access the objB property of class A within innerC WITHOUT passing it along.

This isn't how C# inner classes work, as described in this article: C# nested classes are like C++ nested classes, not Java inner classes

If you want to access A.objB from innerC then you are going to have to somehow pass class A to innerC AND make sure that you expose the objB property somehow. (i.e. this won't work if you have objB set to private.)

Andrew Anderson