I'm still relatively new to C# and posting on a forum but i am trying to make a function that will help me shorten my code to make it more manageable/readable for someone who is new to coding in C# because it scared me when reading so many lines of code.
Ill show what I'm trying to do in code view and explain after just because i am not too good at explaining before hand.
https://gyazo.com/3a440ca6c4f0fc1764f7edd41081e434 - screenshot of the whole code so far.
this is the code in the button.
private void button7_Click(object sender, EventArgs e)
{ //this works and outputs the total value.
/* int A, B, C, D, E, F, G, H, Answer;
A = int.Parse(textBox1.Text);
B = int.Parse(textBox2.Text);
C = int.Parse(textBox3.Text);
D = int.Parse(textBox4.Text);
E = int.Parse(textBox5.Text);
F = int.Parse(textBox6.Text);
G = int.Parse(textBox7.Text);
H = int.Parse(textBox8.Text);
Answer = A + B + C + D + E + F + G + H;
lblTotal1.Text = Answer.ToString();
* */
calc(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, lblTotal1.Text);
}
this is the code in the function.
private void calc(string TextBox1, string TextBox2, string TextBox3, string TextBox4, string TextBox5, string TextBox6, string TextBox7, string TextBox8, string TextAnswer)
{
int A, B, C, D, E, F, G, H, Total;
A = int.Parse(TextBox1);
B = int.Parse(TextBox2);
C = int.Parse(TextBox3);
D = int.Parse(TextBox4);
E = int.Parse(TextBox5);
F = int.Parse(TextBox6);
G = int.Parse(TextBox7);
H = int.Parse(TextBox8);
Total = A + B + C + D + E + F + G + H;
TextAnswer = Total.ToString();
}
At first i thought it was due to using a string but i don't know whether or not this is the issue and if so how to fix it.
Would be a massive help if someone could tell me what i am doing wrong. Many thanks in advance.