Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Elfar Oliver
Elfar Oliver
3,924 Points

Even though I copy him and already define the Cell class, I still get an error

error CS0103: The name `colIndex' does not exist in the current context

for(int rowIndex = 0; rowIndex < sheet.GetLength(0); rowIndex++)
> {
> for(int colIndex = 0; colIndex < sheet.GetLength(1); colIndex++);
> {
> sheet[rowIndex, colIndex] = new Cell();
> }
> }

I wouldn't mind knowing what the heck is up

1 Answer

Try removing the ; after for(int colIndex = 0; colIndex < sheet.GetLength(1); colIndex++); to change it to for(int colIndex = 0; colIndex < sheet.GetLength(1); colIndex++)

for(int colIndex = 0; colIndex < sheet.GetLength(1); colIndex++);
{
    sheet[rowIndex, colIndex] = new Cell();
}

is equivalent to

for(int colIndex = 0; colIndex < sheet.GetLength(1); colIndex++)
{
    // This code block runs for each pass of the for loop
}
{
    // This code block runs after the for loop ends
    sheet[rowIndex, colIndex] = new Cell();
}

Since sheet[rowIndex, colIndex] = new Cell(); is no longer in the for block, colIndex is no longer defined.