jeudi 17 décembre 2015

Infinite while loop skips If blocks in future iterations after Continue keyword used in C#

I am having an issue with my code. The goal is to nest the process of asking a user for input in an infinite loop, which only ends when the correct information is supplied or the user clicks cancel. Below is my code:

while (true) { // Loop until correct values are given or process is canceled.
                                   // Ask for list price
#warning this area not tested (two if statements)
                        if (listPrice < 0) {
                            if (inputList.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                                if (!double.TryParse(inputList.InputValue, out listPrice)) continue; // If value not correct restart loop
                                else break;
                            } else return false; // return from method, test failed (if cancel is pressed). 
                        }

                        if (sewpPrice < 0) {
                            if (inputSEWP.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                                if (!double.TryParse(inputSEWP.InputValue, out sewpPrice)) continue;
                                else break;
                            } else return false; // Test automatically failed if exception was thrown trying to read pricing
                        }
                    }

However, when "continue" is called the entire parent "if" block ("if (listPrice < 0)" and also "if (sewpPrice < 0)"), is skipped over in all future iterations. Each specific "if" block does not get skipped until the nested "continue" statement is called. For example, on the second iteration of the loop, the "if (listPrice < 0)" statement is skipped all together and the loop begins by executing the "if (sewpPrice < 0)" statement.

Also, the method that contains this code is called via the Intermediate window in Visual Studio 2015 (as this method is still in testing).

I hope I was as clear as possible, and any and all help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire