-3

Can anybody tell me what is wrong with this code? VS2012 is rejecting the second foreach statement.

I get

"type or namespace name 'grid' could not be found..." 

and

"invalid token 'foreach' in class..."  

public static void go(DataTable grid)
    {
        foreach (DataRow row in grid.Rows);
    }
           foreach (DataColumn col in grid.columns);
    }

I get the same error for:

public static void go(DataTable grid)
    {
        foreach (DataRow row in grid.Rows);
    }
           foreach (DataColumn col in row.columns);
    }

My VS has been crashing periodically (actually, first real "blue screen of death" that I've seen since before Windows XP) and I've had some unusual behaviors like controls disappearing from forms.

So, who is suffering distorted code logic, me or VS?

4

4 回答 4

3

Your nested foreach block doesn't have closure:

It should be:

public static void go(DataTable grid)
{
    foreach (DataRow row in grid.Rows)
    {
        foreach (DataColumn col in row.columns)
        {
        }
    }
}
于 2013-06-03T23:56:42.037 回答
2

The second foreach seems just randomly placed in your code file. It needs to be inside the function

        public static void go(DataTable grid)
        {
            foreach (DataRow row in grid.Rows)
            {
               foreach (DataColumn col in row.columns)
               {
               }
            }
        }
于 2013-06-03T23:58:05.160 回答
1

You have an extra } right before the second foreach. That one is closing the method, so the second foreach is out of the method definition and that is a syntax error.

于 2013-06-03T23:56:41.597 回答
1

Your code is hard to read without proper indentation, but your foreach's are not nested. That is they are completely separate, so 'grid' is not visible to the second foreach block.

于 2013-06-03T23:57:49.540 回答