Originally Posted by
Grantyt3
I'm relatively new to classes, and I'm trying to figure out something:
I have two classes, and they both need to access a boolean array, but if I declare that array in one of the classes, even if it's public, the other class cannot recognize it. How do I work around/fix this?
Did you instantiate the class? Because, although you can do this by making the array member static, like already mentioned, it probably isn't desired because it'll belong to the whole class? If you dont want this, you could make the arary an instance member and access it with an indexer:
Code:
public class Start
{
public static void Main( )
{
ClassA a = new ClassA( );
ClassB b = new ClassB( );
a[3] = true;
b.Boolean = a[3];
Console.WriteLine(b.Boolean); // prints True
}
}
public class ClassA
{
private bool[] values = new bool[5];
public bool this[int index]
{
get { return values[index]; }
set { values[index] = value; }
}
}
public class ClassB
{
private bool _boolean = false;
public bool Boolean
{
get { return _boolean; }
set { _boolean = value; }
}
}
}