When transferring data, I have a recursion of calls and the stack ends, which is why:
In the Controller_MainForm class there are variables public int cordX, cordY; that need to be passed to the Raket class
Here is what I'm trying to do:
class Raket { Controller_MainForm control; public Raket() { control = new Controller_MainForm(); x = control.cordX; y = control.cordY; } But an instance of the Model class is created in the Controller_MainForm class method Model
public partial class Controller_MainForm : Form { View view; Model model; public int cordX, cordY; Thread modelGo; public Controller_MainForm() : this(850) { } public Controller_MainForm(int sizeField) : this(sizeField, 1) { } public Controller_MainForm(int sizeField, int amountRaket) : this(sizeField, amountRaket, 1) { } public Controller_MainForm(int sizeField, int amountRaket, int amountAero) : this(sizeField, amountRaket, amountAero, 40) { } public Controller_MainForm(int sizeField, int amountRaket, int amountAero, int speedGame) { InitializeComponent(); model = new Model(sizeField, amountAero, amountRaket, speedGame); view = new View(model); this.Controls.Add(view); cordX = Convert.ToInt32(textBox1.Text); cordY = Convert.ToInt32(textBox2.Text); } private void Start_Click(object sender, EventArgs e) { if (model.gameStatus == GameStatus.stoping) { model.gameStatus = GameStatus.playing; modelGo = new Thread(model.Go); modelGo.Start(); view.Invalidate(); } } private void Controller_MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (modelGo != null) { model.gameStatus = GameStatus.stoping; modelGo.Abort(); } DialogResult dr = MessageBox.Show("Закрыть приложение?" , "Radionavig_24", MessageBoxButtons.YesNoCancel); if (dr == DialogResult.Yes) e.Cancel = false; else e.Cancel = true; } } And in the Model class, the method is:
class Model { int sizeField; int amountRaket; int amountAero; public int speedGame; public GameStatus gameStatus; public Raket raket; public Model(int sizeField, int amountRaket, int amountAero, int speedGame) { this.sizeField = sizeField; this.amountAero = amountAero; this.amountRaket = amountRaket; this.speedGame = speedGame; raket = new Raket(); gameStatus = GameStatus.stoping; } public void Go() { while (gameStatus == GameStatus.playing) { Thread.Sleep(speedGame); raket.Run(); } } } does new Raket() , and Raket() again creates an instance of Controller_MainForm and so on in a circle. How do I transfer data without recursion?
static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] arg) { Controller_MainForm cm; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); switch (arg.Length) { case 0: cm = new Controller_MainForm(); break; case 1: cm = new Controller_MainForm(Convert.ToInt32(arg[0])); break; case 2: cm = new Controller_MainForm(Convert.ToInt32(arg[0]), Convert.ToInt32(arg[1])); break; case 3: cm = new Controller_MainForm(Convert.ToInt32(arg[0]), Convert.ToInt32(arg[1]), Convert.ToInt32(arg[2])); break; case 4: cm = new Controller_MainForm(Convert.ToInt32(arg[0]), Convert.ToInt32(arg[1]), Convert.ToInt32(arg[2]), Convert.ToInt32(arg[3])); break; default: cm = new Controller_MainForm(); break; } Application.Run(cm); } }