i have two class i need to declare a variable common to both the classes..
In case of Nested classes i need to access the Outer class variable in the inner class
please give me a better way to do this in c#.
Sample code
Class A
{
int a;
Class B
{
// Need to access " a" here
}
}
Thanks in advance
First suggestion is to pass a reference to the Outer class to the Inner class on construction, so Inner class that then reference Outer class properties.
public Class Class_A
{
int a;
public Class Class_B
{
Class_A instance;
public Class_B(Class_A a_instance)
{
instance = a_instance;
}
void SomeMethod()
{
int someNumber = this.instance.a;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With