tags:

views:

148

answers:

4

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

A: 

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).

Konrad Rudolph
A: 

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 { ... }
}
Craig Eddy
+1  A: 

You can learn about Nested Types here.

itsmatt
+2  A: 

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.

Austin Salonen