Code Unreachable Error in Code As Warnng

Vladan899
if(Screens.Count > 0)
{
    for (int i = Screens.Count -1; i < 0; i +=1)
    {
        if (Screens[i].GrabFocus)
            Screens[i].GrabFocus = true;
        break;
    }
}

Unreachable Code

Trying to convert VB code to C#

For I = FoundScreens - 1 to 0 step -1
    if (Screens[i].GrabFocus)
        Screens[i].GrabFocus = true;
    break;

Where is my mistake in C#?

CheGueVerra

At first glance the VB code starts with the last screen in the list to the first one

VB.NET

For I = FoundScreens - 1 to 0 step -1

To perform the same thing in C#

for (int i = Screens.Count -1; i > 0; i--)

Your original code, was unreachable, because of the loop condition in the for, which you implemented as such:

for (int i = Screens.Count -1; i < 0; i +=1)

i< 0, when i is assigned the number of screens it can be 0 to n, so i will never be smaller than 0.

I'm surprised that you would get such a warning in the for because a simple test of this code

    for (int i = 0; i < 0; i +=1)
    {
        Console.WriteLine(i.ToString());
    } 
    Console.ReadKey();

Will show no warnings in a simple console application.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related