Use of unassigned local variable c# error

我是研究僧i 提交于 2021-01-29 16:00:16

问题


As follows, when i debug it gives me the error : Error 1 Use of unassigned local variable 'moneyBet' I'm not sure what is wrong with the following code. I've never gotten something like that before.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyNotSoVeryFirstApplication
{
class Program
{
    static void Main(string[] args)
    {
        bool stillGoing = true;
        int moneyBet;
        int moneyInBank = 0; //change 0 to amount held in actuality
        while (stillGoing == true)
        {
            Console.WriteLine("Money in bank : {0}", moneyInBank);
            Console.WriteLine("----------------------------------------------------");
            Console.Write("Enter amount you would like to bet: ");
            string moneybetString = Console.ReadLine();
            try
            {
                moneyBet = Convert.ToInt32(moneybetString);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (OverflowException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (moneyBet > Int32.MaxValue)
                Console.WriteLine("You are about to bet {0}. Are you sure you want to bet this amount?", moneyBet);
            }
        }
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey(); 
    }
}

}


回答1:


You need to definitely assign moneyBet before you read it in the line:

if (moneyBet > Int32.MaxValue)

if Convert.ToInt32(moneybetString); throws an exception it will not be assigned.

The specification describes the rules for definite assignment in try/finally blocks:

5.3.3.14 Try-finally statements

For a try statement stmt of the form: try try-block finally finally-block

• The definite assignment state of v at the beginning of finally-block is the same as the definite assignment state of v at the beginning of stmt.

moneybetString is not definitely assigned before the try block and is therefore not definitely assigned at the beginning of the finally block where it is read.

The simplest solution is to assign an initial value at the point of declaration:

int moneyBet = 0;

also be aware your condition will always be false since moneyBet is an int and cannot exceed int.MaxValue.




回答2:


Finally is called no matter what. So, if there is an exception, moneyBit is still called in that if statement. Thus, you get an assignment error because it never got assigned(exception was thrown by Convert.ToInt32(moneybetString)).

Either you need to assign it a value when you declare it or utilize Int32.TryParse.




回答3:


You're assigning moneyBet in the try-block, but if that throws an exception, you're using an unassigned variable in the finally-block. Just change int moneyBet; to int moneyBet = 0; and you're good to go




回答4:


try int moneyBet=0;

In your code, if the try block fails then in the finally block, the moneyBet will remain unassigned.




回答5:


While you declare the variable moneyBet, if an exception was thrown during Convert.ToInt32, moneyBet would remain unassigned as there's no assigning occurring in the catch blocks, therefore by the time you reach the finally block, moneyBet is unassigned.



来源:https://stackoverflow.com/questions/23230787/use-of-unassigned-local-variable-c-sharp-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!