26

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.

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • 3
    Since you wrote most of the code there...why not just dump it into Visual Studio and try it for yourself? – Justin Niessner Jun 02 '10 at 13:07
  • @Justin: Thx for ur help, I am already trying to do this in VS. I just summarized my 100 line of code in what i actually want at present situation. Please suggest if u get any solution. I need to access A's object in my event that is being raised by some other class and is being captured at innerC – Shantanu Gupta Jun 02 '10 at 13:12
  • 1
    I explain it like this to our guys: Imagine you didnt pass outter class's instance to it. And now imagine your outter class has a static field/property of an instance of the inner class. Now imagine your inner class accessing your outer class's fields, properties and methods. It can even access private ones. But whose will it access? You can have xxx instances of the outer class, but that doesnt mean there are the same amout of inner class just because its inner. So the static instance would be just 1 and cant know who/where in memory the outter class is. Inner class is still a normal class. – That Marc Jun 16 '19 at 20:27

3 Answers3

31

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.

John Doe
  • 165
  • 1
  • 4
  • 15
Andrew Anderson
  • 3,409
  • 22
  • 25
  • 18
    You need to pass in the parent (since there's no `outer` keyword), but the inner class **can** access private members of the parent. – dbkk Mar 24 '13 at 15:02
  • 1
    Thanks for the downvote. Also, you're wrong. – Andrew Anderson Mar 28 '13 at 17:09
  • 2
    Ask the C# compiler: [sample code](http://pastebin.com/HwA6F2Ee). – dbkk Mar 30 '13 at 06:07
  • 9
    "Nested types can access private and protected members of the containing type, including any inherited private or protected members." Nested types are within the definition scope of outer private members, therefore can access them. See [this MSDN article](http://msdn.microsoft.com/en-us/library/ms173120(v=vs.110).aspx) – Dave T. May 30 '13 at 13:06
  • However @AndrewAnderson, you are right about passing outer to inner as a constructor argument. – Dave T. May 30 '13 at 13:07
  • dbkk and Dave T. are right. Private properties can also be accessed by the inner class. The only condition is passing the outer object to the inner object. – Racso Oct 11 '17 at 17:05
  • @Racso If you think the answer *(or part of it)* is wrong, you can always address that in the comments and/or downvote. However, you should *not* make an edit that deviates from the author's intent. You can refer to this [meta question](https://meta.stackoverflow.com/q/329801/4934172) for more info. – 41686d6564 stands w. Palestine Oct 12 '17 at 01:04
12

You need to pass a reference of OuterClass to InnerClass, perhaps in the constructor, like:

public class OuterClass
{
    //OuterClass methods

    public class InnerClass
    {
        private OuterClass _outer;

        public InnerClass(OuterClass outer)
        {
            _outer = outer;
        }
    }
}

Then you can use that reference in all of your InnerClass methods.

Mason240
  • 2,924
  • 3
  • 30
  • 46
dlras2
  • 8,416
  • 7
  • 51
  • 90
  • 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 Jun 02 '10 at 13:30
  • Could your event arguments hold a reference to the object you need? – dlras2 Jun 02 '10 at 13:32
  • 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 Jun 02 '10 at 13:35
  • @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 Jun 02 '10 at 13:36
  • 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... – dlras2 Jun 02 '10 at 13:52
-1

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
  • 23,773
  • 22
  • 96
  • 162
  • 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 Jun 02 '10 at 13:17