У меня есть форма CheckersForm и есть класс LogicGame. В CheckersForm я произвожу некие действия, а точнее сохраняю кнопки со значениями в FieldButton[,] fieldButtons; затем когда захожу в LogicGame, то там, чтобы обратиться к CheckersForm, создаю экземпляр этого класс CheckersForm cf = new CheckersForm(); И, естественно, работает всё отлично, только вот данных про мои кнопки в cf уже нет.
Как мне быть? Может свойства использовать? Но я не очень понимаю как.
public partial class CheckersForm : Form
{
const int cellSize = 50;
const int mapSize = 8;
private delegate void printer(string data);
Checker[,] arrChecker;
printer Printer;
FieldButton[,] fieldButtons;
private Thread _clientThread;
public FieldButton[,] FieldButtons { get => fieldButtons; }
Image whiteFigure;
Image blackFigure;
LogicGame lg;
Field field;
public void GameStart()
{
fieldButtons = new FieldButton[mapSize, mapSize];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
FieldButton btn = new FieldButton();
btn.Location = new Point(j * cellSize, i * cellSize);
btn.Size = new Size(cellSize, cellSize);
btn.MouseUp += new MouseEventHandler(FieldButtonClick);
btn.X = btn.Location.X;
btn.Y = btn.Location.Y;
btn.BackColor = GetPrevButColor(btn);
fieldButtons[i, j] = btn;
this.Controls.Add(btn);
}
}
}
public void UpdateBoardd(string data)
{
string coords = data;
var reg = new Regex(@"(?<open>\%).*?(?<final-open>\%)");
var matches = reg.Matches(coords).Cast<Match>()
.Select(m => m.Groups["final"].Value).ToList();
foreach (string strCoords in matches)
{
coords = strCoords;
}
string[] subs = coords.Split(',');
string ast = subs[0];
string bst = subs[1];
int a = int.Parse(ast);
int b = int.Parse(bst);
foreach (var btn in fieldButtons)
{
if ((btn.Location.X == a) && (btn.Location.Y == b))
{
lg.ActionsWithBoard(btn);
}
}
}
А вот LogicGame
public class LogicGame
{
private Socket _serverSocket;
Chat chat;
CheckersForm cf = new CheckersForm();
Field field;
FieldButton[,] arrayChecker;
const int cellSize = 50;
public LogicGame()
{
field = new Field();
arrayChecker = cf.FieldButtons;
//field.GenerateField();
}
Как правильно этим всем управляться, по идее можно было бы сделать класс LogicGame статическим, но тогда там столько кода полетит к чертям, мне кажется static это не выход.