-2

I've been programming a soft on VS in C#. It is an old program made by someone else by me but my problem is in my code I think. In this soft, there is two Form, the 1st one get some datas, and the second make the operations and also provide a report of the datas that I use.

double sum = 0;

for (int k = 0; k <= length - 1; )
{
    sum += Convert.ToDouble(pisteA[k]);
                       
    k += 1;
}

double average = sum / 3;

I have an array containing Text and null values in the 2nd Form. I just want to make an average of this array, so with a for loop, I did what I have to do. But, when I launch my soft, It doesn't go through the line code and stops at this line.

sum += Convert.ToDouble(pisteA[k]);

When I use the Breakpoint to see what's happening, the code go through this line, and then go to my other form to execute this :

formImpression.Show();

To test something, I added a MessageBox.Show just after the line of the sum, and also before, and it seems that the soft execute the code above the problem line, but not the ones that are after this.

I seriously don't have any idea of what's happening.

Edit : For more comprehension about the code I use, I leave it here.

String[] varGen = { formulaire.Form1.varG.piste1, formulaire.Form1.varG.piste2, formulaire.Form1.varG.piste3, formulaire.Form1.varG.piste4, formulaire.Form1.varG.modulesPiste1, formulaire.Form1.varG.modulesPiste2, formulaire.Form1.varG.modulesPiste3, formulaire.Form1.varG.modulesPiste4 };
        TextBox[] pisteA = { mesure1, mesure2, mesure3, mesure4, mesure5, mesure6, moyenne1 };
        TextBox[] pisteB = { mesure7, mesure8, mesure9, mesure10, mesure11, mesure12, moyenne2 };
        TextBox[] pisteC = { mesure13, mesure14, mesure15, mesure16, mesure17, mesure18, moyenne3 };
        TextBox[] pisteD = { mesure19, mesure20, mesure21, mesure22, mesure23, mesure24, moyenne4 };
        double moyenne = 0;
        int indexMesures = 0;
        bool presencepn = false;

        for (int n = 0; n < dataGridView1.Rows.Count; n++)
        {
            if (formulaire.Form1.varG.pn == dataGridView1.Rows[n].Cells[0].Value.ToString() & formulaire.Form1.varG.pn != null)
            {
                presencepn = true;

                //Initialisation
                object module_A1 = dataGridView1.Rows[n].Cells[1].Value;
                object module_A2 = dataGridView1.Rows[n + 1].Cells[1].Value;
                object module_A3 = dataGridView1.Rows[n + 2].Cells[1].Value;
                object module_D1 = dataGridView1.Rows[n].Cells[2].Value;
                object module_D2 = dataGridView1.Rows[n + 1].Cells[2].Value;
                object module_D3 = dataGridView1.Rows[n + 2].Cells[2].Value;

                int moduleA1 = int.Parse(module_A1.ToString())-1;
                int moduleA2 = int.Parse(module_A2.ToString())-1;
                int moduleA3 = int.Parse(module_A3.ToString())-1;
                int moduleD1 = int.Parse(module_D1.ToString())-1;
                int moduleD2 = int.Parse(module_D2.ToString())-1;
                int moduleD3 = int.Parse(module_D3.ToString())-1;

                //Remplissage tableau sans exclusion
                // Piste [1,2,3,4]
                for (int j = 0; j < formulaire.Form1.varG.nbPistes; j++)
                {

                    // Initialisation de la moyenne
                    moyenne = 0;


                    // Valeur pour la piste J
                    for (int i = 0; i < Convert.ToInt32(varGen[j + 4]); i++)
                    {

                        // Calcul de la moyenne
                        moyenne = moyenne + mesures[indexMesures];


                        // Choix de la piste
                        if (varGen[j] == "A")
                        {
                            pisteA[i].Text = Math.Round(mesures[indexMesures], 3).ToString();                       // Remplissage des mesures
                            
                        }
                        else if (varGen[j] == "B")
                        {
                            pisteB[i].Text = Math.Round(mesures[indexMesures], 3).ToString();                       // Remplissage des mesures
                            pisteB[6].Text = Math.Round((moyenne / Convert.ToDouble(varGen[j + 4])), 3).ToString(); // Remplissage de la case moyenne
                        }
                        else if (varGen[j] == "C")
                        {
                            pisteC[i].Text = Math.Round(mesures[indexMesures], 3).ToString();                       // Remplissage des mesures
                            pisteC[6].Text = Math.Round((moyenne / Convert.ToDouble(varGen[j + 4])), 3).ToString(); // Remplissage de la case moyenne
                        }
                        else if (varGen[j] == "D")
                        {
                            pisteD[i].Text = Math.Round(mesures[indexMesures], 3).ToString();                       // Remplissage des mesures
                            pisteD[6].Text = Math.Round(moyenne / Convert.ToDouble(varGen[j + 4]), 3).ToString(); // Remplissage de la case moyenne
                        }
                        // Incrémentation de l'index des mesures
                        indexMesures += 1;
                    }
                }

                int length = pisteA.Length - 1;


                //Suppression des modules exclus
                for (int k = 0; k <= length; k++)
                {
                    if (k == (moduleA1) | k == (moduleA2) | k == (moduleA3))
                    {
                        pisteA[k].Text = null;
                    }
                }

                for (int k = 0; k <= length; k++)
                {
                    if (k == (moduleD1) | k == (moduleD2) | k == (moduleD3))
                    {
                        pisteD[k].Text = null;
                    }
                }
                double sum = 0;
                for (int k = 0; k <= length - 1; )
                {
                    sum += Convert.ToDouble(pisteA[k]);
                    k += 1;
                }
                double moyA = sum / 3;
                pisteA[6].Text = Math.Round(moyA).ToString();
            }
Janis
  • 1
  • 1

1 Answers1

0

Have you this setting checked to see exception ?

Exception setting

Laurent BERTHET
  • 86
  • 1
  • 1
  • 7
  • I don't really know how to access the window you showed me, I'm using VS 2010 so it's quite old.. – Janis Jan 21 '21 at 15:02
  • Try menu Debug -> Windows -> Exception Settings, perhaps it's the same path in VS 2010 – Laurent BERTHET Jan 21 '21 at 15:08
  • I've searched since you showed me that window but unfortunately, I can't seem to find it. – Janis Jan 21 '21 at 15:15
  • So in VS 2010, it's a little different : menu Debug + Exceptions and check 'Thrown' and 'User-unhandled' for the line 'Common Language Runtime Exceptions'. Look at the last answer [link](https://stackoverflow.com/questions/8218249/how-do-i-enable-visual-studio-2010-to-break-when-a-first-chance-exception-happen) – Laurent BERTHET Jan 21 '21 at 15:25
  • Thank you, I checked what you said and when I run my soft, I have an error at that exact line where I was blocked all day. I'll search this problem and see what I can do. Thank you again ! – Janis Jan 21 '21 at 15:30