I'm making a chess game and for the pieces to move I need the variable board and it's located in the Form1 class. I was wondering if there is anyway to call the board variable in my base class so I can use it in my other classes that reference my base class.
This is how code looks (I only included key parts not everything)
public partial class Form1 : Form
{
public string[,] board = new string[8, 8];
}
class Pieces
{
//I want there to be a variable here that will always contain the exact data as the board variable
//I also have other variables that are in the Form1 class that I need in here
}
class Rook : Pieces
{
//This is just a small sample of how I set up the moves but its in a for loop
if (board[x + i, y] == null && !board[x + i, y].Contains("King"))
pieceMove.AddLast(placementBoard[x + i, y]);
}
This is what I've thought of but I want to know if there is a different approach
public partial class Form1 : Form
{
public string[,] board = new string[8, 8];
Rook[] whiteRook = new Rook[10];//I made an array of rooks so when a pawn gets to the opposite side of the board it can turn into a rook
public Form1()
{
InitializeComponent();
Rook[0] whiteRook = new Rook();
whiteRook.board = board;//Everytime a piece moves I will call this with every piece to update it
}
}
class Pieces
{
public string[,] board = new string[8,8];
}
class Rook : Pieces
{
//This is just a small sample of how I set up the moves but its in a for loop
if (board[x + i, y] == null && !board[x + i, y].Contains("King"))
pieceMove.AddLast(placementBoard[x + i, y]);
}
Form1class but i put the board variable to public to try and see if I can access it from the pieces class and just couldn't be asked to remove it. Right now I have it as `public static string[,] board = new string[8,8]; and I copied the movement for the rook and put it in the rook class and it doesn't want to move nowstaticcarefully; because you have created a global you can get unexpected effects if multiple objects/threads interact with it. Its a good tool, but you need to know how to use it and what side-effects there are.staticcode at all and the way I see it is a key word that will change that one variable for the whole class instead of the individual class. So am i using it right?