There's no way the code in the question will ever look at more than the first cell.
The only difference between the two if()
conditions is one uses ==
while the other uses !=
; one of those conditions will always be true. Thus the very first cell will always break out of the loop. It's the same as if you had written this code, with no loop at all:
if (baseline[1, 1]?.ToString() == result_PriceUpdate[1, 1]?.ToString())
goto EndofLoop1;
if (baseline[1, 1]?.ToString() != result_PriceUpdate[1, 1]?.ToString())
goto EndofLoop2;
EndofLoop1:
result_file.WriteResultToExcel("Result Summary", 2, 2, "pass");
EndofLoop2:
result_file.WriteResultToExcel("Result Summary", 2, 2, "fail");
Maybe you're relying on the ?.
null conditional operator to create comparisons with null
, with the idea this will behave the same as it does in SQL, where comparing a value with null
could produce false
for both the !=
and ==
conditions. That won't happen here.
Worse, the jump to EndOfLoop1
doesn't end the code. The EndOfLoop2
section is still part of the method, and it will also run. Once the second section runs, it replaces the work from the first section. Even when you pass, the result you'll see in the file is still "fail".
More than that, if somehow no condition in the loop is ever true, both named sections would still run after the loop finished.
Better practice here is simply don't use goto
. There's no need, and it clearly confused things. Instead, set a string variable to either "pass" or "fail" and change i
and j
to int.MaxValue
so the loop exits right away naturally. Then only have one WriteResultToExcel()
to write out the string variable.
The obvious mistake with the loop means the code in the question is unfortunately not clear enough to determine your real intent. I will suggest a solution based on the idea you want to pass only if all cells pass, and if any cells fails that will fail the entire data set:
string result = "pass";
for (i = 0; i < baseline.GetLength(0); i++)
for (j = 0; j < baseline.GetLength(1); j++)
{
if (baseline[i, j]?.ToString() != result_PriceUpdate[i, j]?.ToString())
{
result = "fail";
i = int.MaxValue;
j = int.MaxValue;
}
}
result_file.WriteResultToExcel("Result Summary", 2, 2, result);
I also have a question about starting with 1, rather than 0. C# arrays start at 0, but you never try to check the 0 positions for either dimension of the arrays, and this seems like a clear mistake.