When debugging, pressing F10 successively reaches comboBox3.SelectedIndex = 1; and then goes not to textBox1.Text = "345"; , and on mas = new double [s]; in another class (second figure). And an error is presented presented in the second figure. int s is equal to a negative value, although this method is called further in the program and the value of s should not be obtained in any way negative. What is the problem?

alt textalt text

Communication class "Signal" with the form:

private void Form1_Load(object sender, EventArgs e) { ... if (comboBox2.SelectedIndex == 0) { if (comboBox1.SelectedIndex == 0) { if (comboBox3.SelectedIndex == 0) { Сигнал SIN = Форм_Син.G_sin(F, n_pd, F_DISCR); userControl11.Length = SIN.size; for (int i = 0; i < SIN.size; i++) { userControl11.x[i] = i / F_DISCR; userControl11.y[i] = SIN[i]; } } else { Сигнал LCHM = Форм_ЛЧМ.G_LHCM(F, F_DISCR, basa, n_pd); userControl11.Length = LCHM.size; for (int i = 0; i < LCHM.size; i++) { userControl11.x[i] = i / F_DISCR; userControl11.y[i] = LCHM[i]; } } } } } 

The second connection of the class "Signal" with the form:

 private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox3.SelectedIndex == 0) { Сигнал SIN = Форм_Син.G_sin(F, n_pd, F_DISCR); userControl11.Length = SIN.size; for (int i = 0; i < SIN.size; i++) { userControl11.x[i] = i / F_DISCR; userControl11.y[i] = SIN[i]; } userControl11.P_ka(); } if (comboBox3.SelectedIndex == 1) { Сигнал LCHM = Форм_ЛЧМ.G_LHCM(F, F_DISCR, basa, n_pd); userControl11.Length = LCHM.size; for (int i = 0; i < LCHM.size; i++) { userControl11.x[i] = i / F_DISCR; userControl11.y[i] = LCHM[i]; } userControl11.P_ka(); } } 
  • one
    Please show how the Signal class is associated with the form. You probably have an event handler for changing the selected index in ComboBox3. When you programmatically change the index of the selected item, the event is also raised and processed, try walking with the F11 button, instead of F10, to make sure of this. - DmitryBLR
  • Added a connection code of the Signal class with the form (wrote in the question field). - Vezd September

1 answer 1

All right, you wrote that this method is called further in the program, which is not true. The event handler is called immediately after you change the selected index. And this means that after each

 ComboBox1.SelectedIndex=0; ComboBox2.SelectedIndex=0; ComboBox3.SelectedIndex=1; 

Called your handler. This means that F, F_DISCR, basa, n_pd have not yet been initialized, that is, they are equal to zero by default. The easiest way for you to move this block

 ComboBox1.SelectedIndex=0; ComboBox2.SelectedIndex=0; ComboBox3.SelectedIndex=1; 

at the end of the Form1_Load method.