I created a program which calculates the sum of a column in a datagridview, the problem arises when I refactor the logic from the click handler into a separate a method. The calcSum
method does not seem to give me the correct output compared with button2_Click
which has all the logic directly in the click handler.
Can anyone please point out what's wrong with my code?
//Calls sum of column method, "incorrect" output
private void button1_Click(object sender, EventArgs e){
calcSum(a,b,3);
}
private double calcSum(double a, double b, int c){
foreach(DataGridViewRow r in dataGridView1.Rows){
a = Convert.ToDouble(r.Cells[c].Value);
b = b + a;
}
MessageBox.Show("sum is " = b.ToString());
return b;
}
//shows correct/calculates output
private void button2_Click(object sender, EventArgs e){
double a =0,b=0;
foreach (DataGridViewRow r in dataGridView1.Rows){
a = Convert.ToDouble(r.Cells[3].Value);
b = b + a;
}
MessageBox.Show(b.ToString());
}