使用未分配的局部变量'novon' [英] Use of unassigned local variable 'novon'

查看:96
本文介绍了使用未分配的局部变量'novon'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'使用未分配的局部变量'novon''是我在尝试使用novon之后得到的错误。



我可以使用像那样的休息或者?



'Use of unassigned local variable 'novon'' is error which I got after I tryed to use novon for something.

Do I can use break like that or?

double nn;
int nekibr;
int novon;



for (int i = 2; i < 1000; i++)
{
    nn = Math.Pow(2, i);
    nekibr = (int)nn - 2;

    if (nekibr > pomeracvr)
    {
        novon = i;
        break;
    }
}

推荐答案

是的,您可以使用关键字 break 喜欢这样。



Yes, you can use the keyword break like that.

EnjoY_Y写道:
EnjoY_Y wrote:

'使用未分配的本地变量'novon''是我尝试使用novon之后得到的错误

'Use of unassigned local variable 'novon'' is error which I got after I tryed to use novon for something.

我在这里解释你的语句,这样如果你试图使用<$ c $就会得到错误c> novon 在for循环后



由于使用未分配的变量会导致不可预测的结果,编译器不允许首先发生这种情况。



您没有初始化 novon 在for循环之前的值,编译器不依赖于if语句中的代码在某个时刻执行的可能性。也许你计划总是这样,但编译器看到它仍然没有在for循环后分配值的可能性。



所以你需要分配初始默认值为 novon 您声明它的位置。如果它不应该在循环/ if语句中分配新值,那么它将在循环后仍然具有该初始值。



edit:reworded。

I interprete your statement here so that you get that error if you try to use novon after the for-loop.

Since using an unassigned variable would lead to unpredictable results, the compiler doesn't allow this to happen in the first place.

You didn't initialize novon with a value before the for-loop and the compiler doesn't rely on the possibility that the code in your if-statement will be executed at some point. Maybe you planned for this to always happen but the compiler sees the possibility that it will still not have a value assigned after the for-loop.

So you need to assign an initial default-value to novon where you declare it. If it shouldn't get a new value assigned in the loop/if-statement then it will still have that initial value after the loop.

edit: reworded.


这是编码的拇指规则。在表达式右侧使用变量之前,必须在使用它的范围内或之前初始化它。



在使用它之前为你的变量'novon'分配一个初始值。



根据你提供的代码,'novon'正在与'i'分配如果条件。编译器认为你也可能永远不输入条件,因此它会抛出错误(使用未分配的变量'novon')。



这种情况是一个有效的方案。从测试角度考虑一下。如果你从未达到代码novon = i,你对输出的期望是什么;
That is a thumb rule of coding. Before you use a variable on the right hand side of an expression, it has to be initialized either inside the scope where it is being used or prior to that.

Just assign an initial value to your variable 'novon', before using it.

As per the code you gave, 'novon' is being assign with 'i' inside an if condition. The compiler thinks that it is also possible that you never enter that if condition and hence the it throws error (use of unassigned variable 'novon').

And this situation is a valid scenario. Just think about it from testing perspective as well. What do you expect as the output if you never reach the code novon = i;


问题不是例外。 '使用未分配的局部变量'novon''

真正的问题是你用你发布的代码样本歪曲了问题。



让我们看看。 局部变量表明这个片段应该放在某种方法中。我们开始做吧。他们我们会看到,没有这样的例外。相反,将报告未声明的变量 pomeracvr 。为什么?



这很明显。这是因为你因上下文欺骗了。 (请不要冒犯。我并不是说你有任何不良意图;我知道这是缺乏或经验和理解的结果。上面的引号是有意的)。您排除了声明 pomeracvr 的片段,并且还排除了可以使用 novon 的一些未知代码。



在您显示的代码片段中,不会显示未分配的局部变量问题,因为 novon 实际上是在使用前分配到处。但是如果您在下面的某些代码中再次使用此变量,则可能会出现此错误。很明显,你的循环并不总是为这个变量赋值。请注意,分配在if下完成。这样做的结果应该是显而易见的。



所以,你的主要问题不仅仅是编写错误的代码,而是理解上下文而不提供准确的观察和代码样品。你不应该做这些误导性的事情。



-SA
The problem is not the exception. 'Use of unassigned local variable 'novon''
The real problem is that you misrepresented the problem by the code sample you posted.

Let's see. "local variable" suggests that this fragment should be put in some method. Let's do it. Them we will see, that there is no such exception. Instead, the undeclared variable pomeracvr will be reported. Why?

This is quite apparent. This is because you "cheated" with context. (Please, no offense. I don't mean any bad intentions on your side; I understand that this is the result of lack or experience and understanding. Quotation marks above are intended). You excluded the fragment where the pomeracvr was declared and also excluded some unknown piece of code where novon could be used.

In the fragment of code you show would not show "unassigned local variable" problem, simply because novon is actually assigned everywhere before use. But this error could appear if you use this variable again in some code below. It's apparent that your loop does not always assign the value to this variable. Note that the assignment is done under "if". The consequence of this should be apparent.

So, again, your main problem is not just writing wrong code, but rather understanding the context and not providing accurate observations and code sample. You should not do such misleading things.

—SA


这篇关于使用未分配的局部变量'novon'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆