Hello,
i'm working on a project, i've faces a simple problem, i just wanna make sure i have the optimum solution.
i need to access encolsing class member from a nested class.
i tried different approaches, alot of them work...i need to know which is better.
Method 1: Using Inheritance:
public class A
{
//this is the member.
private int member1;
//a property (polymorphism).
public int member1Property
{
get
{
return member1;
}
}
//this is the nested class
//note that i used inheritance.
//also not that inheriting from an enclosing class caused me to reach both private and public members, which i dont like.
public class B : A
{
int member1B = member1;
int member1BfromProperty = member1Property;
}
}
Method 2: Using static members:
public class A
{
//this is the static member.
private static int member1;
//a property (polymorphism).
public int member1Property
{
get
{
return member1;
}
}
//this is the nested class
//note that i didn't use inheritance.
//also not that i refered to the static member directly from the nested class.
//however sometimes using static variables causes some problems (ain't i right )
public class B
{
int member1B = member1;
int member1BfromProperty = member1Property; //inaccsesible, generates error
}
}
well is there any better way than those 2 way...i dont seem to like them, as i fear using static members, and this kind of inheritance is stupid since it reaches members with any kind of access modifiers.
thank you in advance,