How to fix compiler error: CS0841 – Cannot use local variable before it is declared, when variable has already been declared outside the loop

huangapple go评论67阅读模式
英文:

How to fix compiler error: CS0841 - Cannot use local variable before it is declared, when variable has already been declared outside the loop

问题

I get a CS0841 compiler error for 3 different variables that are already initialized outside their respective conditional statements. At first I thought it was a simple issue of having my brackets in the wrong place but after double checking my code, it looks like the code within the loop just doesn't recognize the initialization of the variable outside the loop or if statement. Why do I get this issue?

//代码的简化版本。简化后可能不太有意义,但这更清楚地突出了我的问题。

if(R > 10) { 
int [,] MRold = new int[R, 14]; // MRold在这里声明。
//if语句的其余部分.....
}
else{
//else条件......
}

int k = 1; //k被声明。

for(int i = 0; i < MRarray.GetUpperBound(0); i++) {
    if(R > 10) {
        for(int j = 0; j < MRold.GetUpperBound(0); j++) { //这里的MRold会引发0841错误
            int oo; //oo被声明
            for(int CC = 8; CC <= Rows; CC++) {
                oo = CC;
                break;
            }
            // 这里还有一些其他条件语句/循环,嵌套在其中的是这个:
                for(int jj = 2; jj <= 14; jj++) { 
                    OutArray[k, jj] = OutArray[k, jj] + Increment; // k引发错误
                    OutArray2[oo, jj] = OutputArray2[oo, jj] + Increment; // oo引发错误。
                    break;
                }
            // 我在这里关闭了前面提到的其他循环/语句。
        }
    }
}
//基本上,我的问题是最内层的循环无法访问在它们嵌套在的外部循环或条件语句中声明的变量。
//还要注意的是,发生在第一个引发错误的变量实例之后的MRold/oo/k变量不会引发错误。只有在循环/条件语句内第一次声明的变量引发错误。其余的实例都可以编译。
英文:

I get a CS0841 compiler error for 3 different variables that are already initialized outside their respective conditional statements. At first I thought it was a simple issue of having my brackets in the wrong place but after double checking my code, it looks like the code within the loop just doesn't recognize the initialization of the variable outside the loop or if statement. Why do I get this issue?


//Simplified version of the code. It doesn&#39;t make as much sense simplified but this highlights my //issues more clearly. 

if(R &gt; 10) { 
int [,] MRold = new int[R, 14]; // MRold is declared here. 
//rest of if statement.....
}
else{
//the else condition......
}

int k = 1; //k is declared.

for(int i = 0; i &lt; MRarray.GetUpperBound(0); i++) {
    if(R &gt; 10) {
        for(int j = 0; j &lt; MRold.GetUpperBound(0); j++) { //MRold here throws the 0841 error
            int oo; //oo declared
            for(int CC = 8; CC &lt;= Rows; CC++) {
                oo = CC;
                break;
            }
            // A couple other conditional statements/loops here, and nested within them is this:
                for(int jj = 2; jj &lt;= 14; jj++) { 
                    OutArray[k, jj] = OutArray[k, jj] + Increment; // k throws the error
                    OutArray2[oo, jj] = OutputArray2[oo, jj] + Increment; // oo throws error.
                    break;
                }
            // I close the aforementioned other loops/statements here.
        }
    }
}
//Basically my issue is the innermost loops can&#39;t access the declared variables within outer 
//loops or loops/conditions that came before the one they are nested in.
//Also want to note that instances of MRold/oo/k that occur after the very first one that throws the //error don&#39;t throw an error. Only the first instance of the declared variable throws an error //within the loop/conditional statement. The rest of the instances do not throw the same error. 
//They compile fine.

答案1

得分: 0

代码部分不需要翻译。以下是翻译的内容:

快速修正是初始化oo,例如:

// 无论是晴天还是雨天,都会分配oo
int oo = 0;

for (int CC = 8; CC <= Rows; CC++) {
  // 在某些条件下可以更改oo的值
  oo = CC;

  break;
}

...

// 保证oo已分配一个值
OutArray2[oo, jj] = OutputArray2[oo, jj] + Increment;

因为编译器不理解for循环将至少运行一次,所以oo在第一次使用之前就会被分配。下一步是考虑是否需要for循环?在输入后立即进行breakint CC = 8; ... oo = CC; break;,为什么不将其写为:

int oo = 8;

请检查Increment是否也已经分配(我看不到您的代码中的Increment声明)。

英文:

The quick amendment is to initialize oo, e.g.

// Rain or shine, oo is assigned 
int oo = 0;

for (int CC = 8; CC &lt;= Rows; CC++) {
  // oo can be change under some conditions
  oo = CC;

  break;
}

...

// oo is guaranteed to have a value assigned
OutArray2[oo, jj] = OutputArray2[oo, jj] + Increment;

since compiler doesn't understand that since for loop will be run at least once, oo will be assigned before its first usage. The next stage is to think on do you want the for loop at all? You break just after the entry: int CC = 8; ... oo = CC; break;, why don't you put it as

int oo = 8;

Check if Increment is assigned as well (I can't see Increment declaration in your code)

huangapple
  • 本文由 发表于 2023年2月24日 16:43:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定