Using an unassigned local variable?

Visual Studio continues to say Use of unassigned variable for iVal and iNumber . Can someone tell me where I am going wrong?

This code is intended to ask the user to continue to enter integers and add them until the user wants to stop. Then the sum of the integers is displayed on the console.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AddFive { class Program { static void Main(string[] args) { int iNumber; int iVal; int iTotal = 0; while (iVal > 0) { Console.WriteLine("Enter number " + iNumber); iVal = Convert.ToInt32(Console.ReadLine()); iTotal = iTotal + iVal; } if (iNumber <= 0) { Console.WriteLine("Total = " + iTotal); iVal = Convert.ToInt32(Console.ReadLine()); iTotal = iTotal + iVal; } Console.WriteLine("Total = " + iTotal); Console.WriteLine(); Console.WriteLine("Press any key to close"); Console.ReadKey(); } } } 
+4
source share
6 answers

Assign values ​​to these variables. Before use, you need to assign values ​​to local variables

  int iNumber = 0; int iVal = 0; 

when you wrote while (iVal > 0) , the value of iVal not set

You can only avoid this with the instance / class variable, since they are initialized to the default value.

 public class Program { int i; //this was not implicitly initialized to zero (0) public Program() { int j; //need to initialize this before use Console.Write(j); //this throws "Use of unassigned variable" error Console.Write(i); //this prints 0, the default value } } 
+7
source

Visual Studio is correct, you are trying to reference an uninitialized variable.

Try the following:

  int iNumber = 0; int iVal = 0; 

Thus, you initialize the variables with the initial value 0. The original problem arises in these lines:

while (iVal > 0) as well as if (iNumber <= 0)

In which you are trying to access variables before giving them a value.

+5
source

In C #, you must assign a value to a variable before using it.

eg.

  int iNumber = 0; int iVal = 0; 
+3
source

You need to initialize iNumber and iVal . Think about what value they will have for the first time through a while , in your current code.

+3
source

Your iVal parameter is not assigned in your while loop. you need to specify a value when you initialize it.

+3
source

The problem, as noted in several places that you do not assign iNumber or iVal, before using them for the first time (in your while operations). In this particular case, it is benign and assigns a default value that changes the thing. A mistake, though appropriate. Historically unassigned variables have been a headache in languages ​​that allow the use of unassigned variables. Especially in languages ​​that do not initialize the default storage location. In this case, C # initializes the default value, but may still make it difficult to find errors. The compiler is smart enough to check the path that the code takes before using the specific use of the local one, and if you can get there without assigning a value that it will complain about. This can help in complex code, where the code when reading sequentially leads to the fact that you think that the locale is assigned, but in fact, due to conditional logic, this is not

0
source

Source: https://habr.com/ru/post/1443953/


All Articles