-4

I have the following code:

private void G1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {

            decimal quantity = 0, cost = 0;
            decimal   totalstock = 0, newtotalstock = 0;
            if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["Cost"].Value.ToString(), out cost))
            {
                decimal price = quantity * cost;
                G1.Rows[e.RowIndex].Cells["Total"].Value = price.ToString();
            }

            if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["TotalStock"].Value.ToString(), out totalstock) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity))
            {
                newtotalstock = totalstock - quantity;
                G1.Rows[e.RowIndex].Cells["TotalStock"].Value = newtotalstock.ToString();
                return;
            }
            decimal avai = 0, newavai = 0;
            if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["RealExport"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["AvailableStock"].Value.ToString(), out avai))
            {
                newavai = avai - quantity;
                G1.Rows[e.RowIndex].Cells["AvailableStock"].Value = newavai.ToString();
                return;
            } }

The problem is, it only execute 2 out of 3 the code, I mean, when the newtotalstock is calculated, the code will end.

I try to change the newavai to up above, and the result is the same, it will calculate the newavai and pass the newtotalstock. I dont know why, all the code are correct. Please help

Serenade
  • 50
  • 1
  • 11
  • 5
    Maybe that's because of `return;` statement in the end of `if` block ? – Fabjan Apr 13 '17 at 07:45
  • Debugger is your friend. – Filburt Apr 13 '17 at 07:46
  • @Filburt oh dear, when I bring the question in here, that means I already debugged over 10 times – Serenade Apr 13 '17 at 07:50
  • 1
    I doubt so. You would have seen that the last statement is `return`. – Patrick Hofman Apr 13 '17 at 07:53
  • I gave you upvote, it's a good question and some beginners can learn something from it :) Sadly, a lot of people on this forum can't understand it ( – Arkadiusz Raszeja Apr 13 '17 at 08:25
  • Debugging doesn't mean simply *running* your code, it means *stepping* through every single statement. Set a breakpoint at the very beginning of your method and hit F10 to find out what really happens. If you had done this, you could have come up with a more specific problem than *"The Code Won't Execute all"* – Filburt Apr 13 '17 at 08:31

2 Answers2

1

word "return" ends function, if you are using void type method you don't really need to use return, unless you want to quit it at certain point. The code after "return" will never be executed (only exception might be during usage "yield return", but that is another story).

void someMethod()
{
  doSomething();
  return;
  StartWorldWarThree(); //no waries, this will never be executed :)
}

Furethermore you can always make a breakpoint in your code (click on the left border of your window, or just read about it) and then check how your code is being executed :) F10/F11 to make step in your code, F5 to go to next breakpoint or end execution, if there are no more breakpoints.

1

Taking in count that all conditions are true the return; will stop executing the rest of the code, if you want the 3 if to be executed, remove the return; and place it the last line of the method as

void Method() {
  if() {}
  if() {}
  if() {}
  return;
}

Or dont place it at all, because that method is void and does not need it

MrVoid
  • 709
  • 5
  • 19