简体   繁体   中英

This variable doesn't exist in the current context

string[,] cusName = { { "John", "Smith" }, { "Will", "James"} , { "Bryan", "Doe" } };

for (int x = 0; x < cusName.GetLength(x); x++)
{
    for (int j = x + 1; j < cusName.GetLength(j); j++)
    {
        Console.Write("{0} ", cusName[x, j]);
    }
    Console.Write("{0} ", cusName[x, j]);
}

My problem is that the last Console.Write always produces an error. It says that the variable j doesn't exist in the current context.

As others already pointed out in the comments, you cannot access j outside of the loop it was being declared for. So you would either have to declare j before the loop, or access the last index in a different way than reusing j .

That being said, it's unclear what you want to do with your code, and it's likely that you don't actually want to have that Console.Write there at all.

There is a big problem with your code btw:

for (int x = 0; x < cusName.GetLength(x); x++)

This for-loop will increment x as long as it is smaller than cusName.GetLength(x) . Array.GetLength is used to get the element count of a dimension in a multi-dimensional array. Since your array has two dimensions, the only valid calls you could make to GetLength are GetLength(0) for the first dimension, and GetLength(1) for the second dimension. Having a variable in there, especially one that increments within a loop, is very likely to break.

I assume that you originally wanted to do something like this:

for (int x = 0; x < cusName.GetLength(0); x++)
{
    for (int j = 0; j < cusName.GetLength(1); j++)
    {
        Console.Write("{0} ", cusName[x, j]);
    }
}

If you wanted to continue using a loop variable after the loop, you could declare it before:

int j;
for (j = 0; j < cusName.GetLength(1); j++)
{
    …
}
Console.Write(j);

Note that after the loop, j has the value cusName.GetLength(1) since that is the way loops work: After every loop iteration, the variable will be incremented, and the loop body will be executed if the condition ( j < cusName.GetLength(1) ) is true. So the for loop will always end with another incrementation of the loop variable.

This means that if you use the loop variable as an index, then that index will be out of bounds for the array after the loop. So you should keep that in mind if you want to reuse the variable.

Often, it's easier and clearer if you don't use the loop variable but instead calculate the last index separately again:

cusName[x, cusName.GetLength(1) - 1])

If I want to have like semi global variable for loops. then I should just use a while? Am I correct?

Not quite. The type of loop used and the scope of variables are not as closely related as you think.

The first "argument" of a for loop is meant to be used for a variable declaration and initialization, but actually you can (ab)use it to execute any valid statement. (don't! its not the way to write clean and maintainable code). You can even leave it blank and use a variable you declared earlier, elsewhere:

var j = 42;
for(; j > 0; --j)
{
    /* do something */
}
/* now, j is still in scope */
Console.WriteLine(j);

This is strongly discouraged because it is less readable than the normal syntax, and you would need to know one more obscure bit of information: the value of the loop variable after the last loop execution (it is the final value determined by the break condition plus one increment/decrement) - this is easy to get wrong and introduce one-off-errors.

Few C# beginners know that C# loop syntax (which has many similarities with C) is not constrained to the for(declaration; condition; increment) pattern at all. The following is valid and working code, if you wanted to start an obfuscated C# code competition:

for (int i = 11, j = new Random().Next(33) + 1; i * j < 65536; Console.WriteLine(++j))
{
    ++i;
}

When you define a variable inside a for loop, as in for(int j=...) , that variable exists only inside the loop. If you want to access that variable outside the loop, declare it outside:

int j;
for(j = ...)

you should modify your code as below:

    string[,] cusName = { {"John", "Smith"}, {"Will","James"}, { "Bryan", "Doe" } };

    int j=0;

    for(int x = 0; x < cusName.GetLength(x); x++)
    {
        for (j = x + 1; j < cusName.GetLength(j); j++)
        {

            Console.Write("{0} ", cusName[x,j]);

        }
        Console.Write("{0} ", cusName[x, j]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM