In WPF I face this problem frequently when I bind list to DataGrid and DataContext = new A();
class A
{
int x;
List<B> list;
class B
{
B()
{
// want to use x here, but i can't
}
}
}
Please suggest something
In WPF I face this problem frequently when I bind list to DataGrid and DataContext = new A();
class A
{
int x;
List<B> list;
class B
{
B()
{
// want to use x here, but i can't
}
}
}
Please suggest something
The reason you cannot access it is that you cannot access an instance variable of a class from an inner class directly. Think a little bit about it; how would an instance of class B know which instance of class A to use to read the value of variable x?
In order to access it, you need to provide an instance of class A to the ctor of B. Another means of doing this (if it suits your scenarios) would be to make x static, but I would not suggest it generally.
For an example see this:
class A
{
int x;
List<B> list;
class B
{
B(A instance)
{
// Access x here using A.x;
}
}
public void AddToList()
{
list.Add(new B(this));
}
}