Click here to Skip to main content
16,016,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
'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?

C#
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;
    }
}
Posted
Updated 27-May-15 6:48am
v2

Yes, you can use the keyword break like that.

EnjoY_Y wrote:
'Use of unassigned local variable 'novon'' is error which I got after I tryed to use novon for something.
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.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 27-May-15 15:16pm    
Sascha, your answer is at best unclear. What do you mean by "break"? Your post leaves some impression that you could have swallowed the bait and did not pay attention that the problem is that the inquirer did not describe the observation accurately and did not show important context of the code sample. Of course, I'm 100% sure that you understand things correctly, but how some naive beginners can understand it? Note that as the question is way too trivial, target audience will be some absolute beginners, including the inquirer.

The "edit" part is also misleading. No, "maybe you know for yourself" could not reasonably take place. It's apparent that this depends on the value of pomeracvr. It would be theoretically possible to "know" something only if it was constant. The even automatic static analysis of the code could prove it's not a bug; but of course the compiler does not do it. The explanation based on the programmer's "knowledge" does not look convincing.

Please see Solution 3.

—SA
Sascha Lefèvre 27-May-15 15:50pm    
The use of "break" is part of his question :) But I reworded the rest of it. I assume "pomeracvr" is a class member and he forgot to mention it because it's not part of the error message he's getting.
/Sascha
Sergey Alexandrovich Kryukov 27-May-15 15:53pm    
And this is exactly what was wrong. It makes the question uncertain, not just the assignment of "novon"...
—SA
Sascha Lefèvre 27-May-15 16:02pm    
Yes. I gave him the benefit of the doubt ;)
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;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-15 15:19pm    
No, this is nothing to do with "thumb rule". Your answer is only half correct. Of course you would personally do everything correctly, but your explanation is at best unclear, because you did not address the root of the problem: wrong code context show. If the code was what is shown, this error would not appear, and another one would appear — please see Solution 3.
—SA
Abhipal Singh 27-May-15 22:21pm    
I assumed that 'novon' was getting used outside the for loop. (Actually I was trying to generate that error subconsciously :p and hence gave the solution which turned out to be unclear.. :(.. )

Thanks for your input SA.!!! I appericiate your feedback :)

I will not assume anything and I will ask the inquirer to be specific.

@Inquirer: Please post a complete piece of code which can individually run so that people (like me and you) can help better
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
 
Share this answer
 
You are getting this error because you are using novon variable somewhere after the loop and there are chances that this variable in not assigned any value.
however you can assign 'novon' initially with some default value like 0.
and at the time of using it you can compare with default assigned value to skip if required.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900