Hey. An "Unhandled Exception of the System.StackOverflowException" Error Occurs

On this line: Form1 Control = new Form1 ();

I refer to the controls in my class from Form1.Designer.cs

Code

namespace PC_Atmega161 { class Common { Form1 Control = new Form1(); UART UART_copy = new UART(); public void init() { // Настройка кнопки отправки запроса //buttonSend.Width = dataGridView1.Width; // Настройка ComboBox string[] ComPortNames = SerialPort.GetPortNames(); for (int i = 0; i < ComPortNames.Length; i++) { Control.comboBox1.Items.Add(ComPortNames[i]); } } public void ShowInMessage() { Console.WriteLine("RowCount" + Control.dataGridView2.RowCount); for (int i = Control.dataGridView2.RowCount; i >= 1; i--) { Console.WriteLine("i: " + i); if (i == 1) { break; } Control.dataGridView2.Rows[i - 1].Cells[0].Value = Control.dataGridView2.Rows[i - 2].Cells[0].Value; } Control.dataGridView2.Rows[0].Cells[0].Value = UART_copy.InMessage[0].ToString("x"); Control.dataGridView2.Rows[0].Cells[1].Value = UART_copy.InMessage[1].ToString("x"); Control.dataGridView2.Rows[0].Cells[2].Value = UART_copy.InMessage[2].ToString("x"); Control.dataGridView2.Rows[0].Cells[3].Value = UART_copy.InMessage[3].ToString("x"); Control.dataGridView2.Rows[0].Cells[4].Value = UART_copy.InMessage[4].ToString("x"); Control.dataGridView2.Rows[0].Cells[5].Value = "0" + UART_copy.InMessage[5].ToString("x"); Control.dataGridView2.Rows[0].Cells[6].Value = "0" + UART_copy.InMessage[6].ToString("x"); } } } 
  • It is best to run in debug. Then, when the exception is "miscarried", you will see in the CallStack window exactly which "pranks" method. - Ivan Navoznov
  • 3
    Well, if it throws out when creating an instance of Form1, then look for a problem in the constructor of the Form1 class, and not in this code that you posted! - MDJHD

0