Home > Enterprise >  C# windows form project Divide variable error
C# windows form project Divide variable error

Time:01-22

Greeting everyone, I'm trying to do a windows form for collage were I can get the average of student scores. Seems like I have an issue with the divide function. here is my code , to see if you guys spot something wrong that could help me. I Appreciate in advance

    private void getaveragebutton_Click(object sender, EventArgs e)
    {
        if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
            customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
            || customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
            customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
        {
            MessageBox.Show("Fill all the Boxes with Digits");
        }

        else
        {

            label4.Visible = true;
            label5.Visible = true;
            label7.Visible = true;
            label61.Visible = true;
            label28.Visible = true;
            label29.Visible = true;
            label30.Visible = true;
            label31.Visible = true;
            label45.Visible = true;
            label46.Visible = true;


            //short--test calculations

            //students short test score variables
            decimal a, b, c, d, e1, f, g, h, i, j;
            //students short test value variables
            decimal k, l, m, n, o, p, q, r, s, t;
            //st results variables
            decimal u, v;
            decimal r1st;
            decimal r2st;
            decimal results;

            //customboxes st score
            a = Decimal.Parse(customTextboxes4.Texts);
            b = Decimal.Parse(customTextboxes5.Texts);
            c = Decimal.Parse(customTextboxes6.Texts);
            d = Decimal.Parse(customTextboxes7.Texts);
            e1 = Decimal.Parse(customTextboxes8.Texts);
            f = Decimal.Parse(customTextboxes9.Texts);
            g = Decimal.Parse(customTextboxes10.Texts);
            h = Decimal.Parse(customTextboxes11.Texts);
            i = Decimal.Parse(customTextboxes12.Texts);
            j = Decimal.Parse(customTextboxes13.Texts);

            //custom boxes st values
            k = Decimal.Parse(customTextboxes15.Texts);
            l = Decimal.Parse(customTextboxes16.Texts);
            m = Decimal.Parse(customTextboxes17.Texts);
            n = Decimal.Parse(customTextboxes18.Texts);
            o = Decimal.Parse(customTextboxes19.Texts);
            p = Decimal.Parse(customTextboxes20.Texts);
            q = Decimal.Parse(customTextboxes21.Texts);
            r = Decimal.Parse(customTextboxes22.Texts);
            s = Decimal.Parse(customTextboxes23.Texts);
            t = Decimal.Parse(customTextboxes24.Texts);

            r1st = a   b   c   d   e1   f   g   h   i   j;
            r2st = k   l   m   n   o   p   q   r   s   t;
            //st results variables definitions
            u = Decimal.Parse(label28.Text);
            v = Decimal.Parse(label29.Text);
           
           
            //st result



            label28.Text = r1st.ToString();
            label29.Text = r2st.ToString();
            results = u / v;
            label4.Text = results.ToString();
           
            return;
        }

[enter image description here](https://i.stack.imgur.com/xu8cp.png)

Im trying to get all the score inserted in the text boxes to sum them. then get the results to Student Score and Total (That part is working for me) . After that divide the Student Score / Total to get the results in a hidden label that will be visible after getting the results. (that is the part that Im having issues with)

CodePudding user response:

Well sorry to everyone for not following the proper way to post to get the proper help and thanks for those who let me know what to do for a next time to get the proper help. Also , thank you for explaining me the variable thing that was empty I really appreciate. Here is how I did fix my issue:

    private void getaveragebutton_Click(object sender, EventArgs e)
    {
        if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
            customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
            || customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
            customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
        {
            MessageBox.Show("Fill all the Boxes with Digits");
        }

        else
        {

            label4.Visible = true;
            label5.Visible = true;
            label7.Visible = true;
            label61.Visible = true;
            label28.Visible = true;
            label29.Visible = true;
            label30.Visible = true;
            label31.Visible = true;
            label45.Visible = true;
            label46.Visible = true;


            //short--test calculations

            //students short test score variables
            decimal studentscorebox1, studentscorebox2, studentscorebox3, studentscorebox4, studentscorebox5, studentscorebox6, studentscorebox7, studentscorebox8, studentscorebox9, studentscorebox10;
            //students short test value variables
            decimal studentshorttestvalue1, studentshorttestvalue2, studentshorttestvalue3, studentshorttestvalue4, studentshorttestvalue5, studentshorttestvalue6, studentshorttestvalue7, studentshorttestvalue8,
                studentshorttestvalue9, studentshorttestvalue10;
            //st results variables
            
            decimal studentshortestoverallscore;
            decimal shortestoverallvalue;
            decimal shortestaverage;

            //customboxes student short test scores
            studentscorebox1 = Decimal.Parse(customTextboxes4.Texts);
            studentscorebox2 = Decimal.Parse(customTextboxes5.Texts);
            studentscorebox3 = Decimal.Parse(customTextboxes6.Texts);
            studentscorebox4 = Decimal.Parse(customTextboxes7.Texts);
            studentscorebox5 = Decimal.Parse(customTextboxes8.Texts);
            studentscorebox6 = Decimal.Parse(customTextboxes9.Texts);
            studentscorebox7 = Decimal.Parse(customTextboxes10.Texts);
            studentscorebox8 = Decimal.Parse(customTextboxes11.Texts);
            studentscorebox9 = Decimal.Parse(customTextboxes12.Texts);
            studentscorebox10 = Decimal.Parse(customTextboxes13.Texts);

            //student short test value
            studentshorttestvalue1 = Decimal.Parse(customTextboxes15.Texts);
            studentshorttestvalue2 = Decimal.Parse(customTextboxes16.Texts);
            studentshorttestvalue3 = Decimal.Parse(customTextboxes17.Texts);
            studentshorttestvalue4 = Decimal.Parse(customTextboxes18.Texts);
            studentshorttestvalue5 = Decimal.Parse(customTextboxes19.Texts);
            studentshorttestvalue6 = Decimal.Parse(customTextboxes20.Texts);
            studentshorttestvalue7 = Decimal.Parse(customTextboxes21.Texts);
            studentshorttestvalue8 = Decimal.Parse(customTextboxes22.Texts);
            studentshorttestvalue9 = Decimal.Parse(customTextboxes23.Texts);
            studentshorttestvalue10 = Decimal.Parse(customTextboxes24.Texts);

            //Sumatory of Students scores and Sumatory of Test Value
            studentshortestoverallscore = studentscorebox1   studentscorebox2   studentscorebox3   studentscorebox4   studentscorebox5   studentscorebox6   studentscorebox7   studentscorebox8   studentscorebox9   studentscorebox10;
            shortestoverallvalue = studentshorttestvalue1   studentshorttestvalue2   studentshorttestvalue3   studentshorttestvalue4   studentshorttestvalue5   studentshorttestvalue6   studentshorttestvalue7  
                studentshorttestvalue8   studentshorttestvalue9   studentshorttestvalue10;
          

            //String Results to Labels

            label28.Text = studentshortestoverallscore.ToString();
            label29.Text = shortestoverallvalue.ToString();
            //Get Average of Short Test
              shortestaverage = studentshortestoverallscore / shortestoverallvalue * 100;
              label4.Text = shortestaverage.ToString();

            return;
        }

    }

I did a correction of the variable names to not get confused like I was advised in the first post , then I deleted some variables that were empty like someone here explained me (thanks). Finally I did a re order of how it should be calculated to get the average and got it working. thanks to all and if you see something that I can improve let me know. I really appreciate everyone and every single advise. Have a good day everyone.

CodePudding user response:

You cannot convert label to decimal as long as label contains no value. You have to rearrange the lines as follows

        //st result

        label28.Text = r1st.ToString();
        label29.Text = r2st.ToString();

        u = Decimal.Parse(label28.Text);
        v = Decimal.Parse(label29.Text);

        results = u / v;
        label4.Text = results.ToString();
       
        return;
    
  •  Tags:  
  • c#
  • Related