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); } } 
  • one
    And why do you create class objects? In any case, you will get recursion and stackoverflow. Transfer the class object to the constructor and take the parameter values ​​from it. - V. Dmitriy
  • @ V.Dmitriy tell me, please, how to implement it? - Saint

1 answer 1

I do not understand well the dependency and hierarchy of your classes, but you can implement the transfer of an object to the constructor like this:

Raket class:

 class Raket { Controller_MainForm control; public Raket(Controller_MainForm controller) { this.control = controller; x = control.cordX; y = control.cordY; } 

Controller_MainForm class:

 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, this); view = new View(model); this.Controls.Add(view); cordX = Convert.ToInt32(textBox1.Text); cordY = Convert.ToInt32(textBox2.Text); } 

Model class:

  public Model(int sizeField, int amountRaket, int amountAero, int speedGame, Controller_MainForm controller) { this.sizeField = sizeField; this.amountAero = amountAero; this.amountRaket = amountRaket; this.speedGame = speedGame; this.raket = new Raket(controller); } 

Your Controller_MainForm() constructor does not match the constructor presented in your listing. Therefore, I write almost at random.

Write in the comments if something is wrong.

  • one
    @Saint Fixed, see if this option will work for you - V. Dmitriy
  • one
    @Saint tried to fix. Try it) - V. Dmitriy
  • one
    @Saint suggest that you need to update the form when updating the coordinates. - V. Dmitriy
  • one
    @Saint Well, it's up to you to decide.)) But in theory, you can add a panel and update only the panel :) - V. Dmitriy
  • one
    @Saint System.Windows.Forms.Panel - V. Dmitriy