Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLR is optimising my forloop variables away

I'm trying to run a basic loop that will find a specific value in a dataview grid. I cannot figure out whats going on with the code, since the for loop exits before evaluating its basic condition.

private void SearchDataViewGrid(string FileName)
    {
        //finds the selected entry in the DVG based on the image
            for (int i = 0; i == dataPartsList.Rows.Count ; i++)
            {
                if(FileName == dataPartsList.Rows[i].Cells[3].Value.ToString())
                {
                dataPartsList.Rows[i].Selected = true;
                }
            }
        }

The program doesn't crash, but i get an error on my 'i' variables declaring that it has been optimised away. Tried a few easy fixes i found online but nothing seems to keep it.

I have verified that the string i am passing is the correct one, and my 'dummy' DVG returns a value of 14 for the number of rows contained. Even if i remove the 'if' statement inside of the for loop, i still get the same error.

like image 408
Lee Harrison Avatar asked Nov 23 '25 05:11

Lee Harrison


2 Answers

The condition cond in the middle of for(init; cond; update) is not an until condition but a while condition.

So you need to change it to

 for (int i = 0; i < dataPartsList.Rows.Count ; i++)
like image 166
Henk Holterman Avatar answered Nov 24 '25 19:11

Henk Holterman


The problem is your conditional is i == dataPartsList.Rows.Count so the body will only execute when these two values are equal. This guarantees your loop will never execute. You need to change your conditional to be < instead of ==

for (int i = 0; i < dataPartsList.Rows.Count ; i++) {
  ...
}
like image 43
JaredPar Avatar answered Nov 24 '25 19:11

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!