If I create a private nested class, how can I access the variables and methods from the calling class?
Example:
public class ClassA
{
protected int MyVar=100;
public MethodA()
{
// <some code>
myObjectClassB.DoSomething();
// <some code>
}
private class ClassB
{
public DoSomething()
{
}
}
}
In the above example I need ClassB to be able to access ClassA.MyVar – Is this possible?
When you construct an instance of ClassB, give it a reference to the ClassA that owns it.
One interesting thing to note about this is that the private nested class can actually access private members of ClassA through _owner. This often comes in handy when you have an internal helper class that needs access to the overall private state of the class.