0

I have a nested class. I want to access the outer and nested classes in other class. How to access both class properties and methods and my condition is i want to create object for only one class plz provide the code snippet

3
  • LOL - uh, why don't you provide a bit of code so we can help you. Help me live..."plz provide the code snippet"...LOL Commented Sep 26, 2008 at 17:39
  • which language? can you give a concrete example of what you're trying to achieve? Commented Sep 26, 2008 at 17:39
  • I don't think the question should be modded down, but could the poster please clarify by writing a plain language explanation of what you are trying to do? Commented Sep 26, 2008 at 17:50

4 Answers 4

3

In my opinion, the reason to nest a class is that it will only ever be used by its parent class. If you need to access the inner class, you should revisit using the nested class in the first place.

Sign up to request clarification or add additional context in comments.

Comments

2

You can learn about Nested Types here.

Comments

0

Your question isn't very clear. However, I guess you're from a Java background. C#'s and VB's nested classes behave much different from Java's nested classes. In fact, they behave much like Java's static nested classes, i.e. they don't belong to an instance of the outer class. Therefore, instances of the inner class can't access nonstatic fields in the outer class (at least not without being given an instance explicitly).

Comments

0

Although I would never recommend nested public classes, here's some code:

public class Foo() {
    public Foo() { }

    private Bar m_Bar = new Bar();  

    public Bar TheBar { get { return m_Bar; } }

    public class Bar { ... }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.