Click here to Skip to main content
15,891,875 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a windows form with 5 fields of textbox fields,

depends on if the field has a mark or not it will calculate the average

For E.G if first two field only had marks then

Average = first two field marks calculation/2

averageHeader = 6-4(4 empty fields);

but this is not working , end of the code tot is underlined with red

What I have tried:

private void button10_Click(object sender, EventArgs e)
       {

           int s1, s2, s3, s4, s5, s6;
           int tot;

           int avgHeader = 6;


           if(textBoxS1.Text=="")
           {
               avgHeader = avgHeader - 1;
           }
           else
           {
               s1 = Convert.ToInt16(textBoxS1.Text);
               tot = tot + s1;


           }
           if(textBoxS2.Text=="")
           {
               avgHeader = avgHeader - 1;
           }
           else
           {
               s2 = Convert.ToInt16(textBoxS2.Text);
               tot=tot+s2;
           }
           if (textBoxS3.Text == "")
           {
               avgHeader = avgHeader - 1;
           }
           else
           {
               s3 = Convert.ToInt16(textBoxS3.Text);
               tot = tot + s3;
           }
           if (textBoxS4.Text == "")
           {
               avgHeader = avgHeader - 1;
           }
           else
           {
               s4 = Convert.ToInt16(textBoxS4.Text);
               tot = tot + s4;
           }
           if (textBoxS5.Text == "")
           {
               avgHeader = avgHeader - 1;
           }
           else
           {
               s5 = Convert.ToInt16(textBoxS5.Text);
               tot = tot + s5;
           }
           if (textBoxS6.Text == "")
           {
               avgHeader = avgHeader - 1;
           }
           else
           {
               s6 = Convert.ToInt16(textBoxS6.Text);
               tot = tot + s6;
           }

           int Average = tot / avgHeader;

           txtaverage.Text = Convert.ToString(Average);

       }
Posted
Updated 22-Mar-17 0:03am
v2
Comments
Tomas Takac 22-Mar-17 3:44am    
You didn't say what's wrong with your code? Can you post the expected vs. actual results?
Member 13049972 22-Mar-17 6:04am    
end the end tot is showing red color underline so its not working

1 solution

try

private void button10_Click(object sender, EventArgs e)
       {
           int tot = 0;
           int avgHeader = 0;
           TextBox[] allBox = new TextBox[] { textBoxS1, textBoxS2, textBoxS3, textBoxS4, textBoxS5, textBoxS6 };
           foreach (TextBox txt in allBox)
               if (txt.Text != "")
               {
                   int temp;
                   int.TryParse(txt.Text.Trim(), out temp);
                   tot += temp;
                   avgHeader++;
               }
           int Average = tot / avgHeader;
           txtaverage.Text = Average.ToString();
       }


using LINQ

private void button10_Click(object sender, EventArgs e)
       {
           TextBox[] allBox = new TextBox[] { textBoxS1, textBoxS2, textBoxS3, textBoxS4, textBoxS5, textBoxS6 };
           var avg = allBox.Where(k => k.Text != "").Average(k => Convert.ToInt32(k.Text));
           txtaverage.Text = avg.ToString();
       }


Note: Non numeric text format should be validated at front end
 
Share this answer
 
v2
Comments
Member 13049972 22-Mar-17 7:16am    
Thank Bro its working :)
Karthik_Mahalingam 22-Mar-17 7:29am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900