简体   繁体   中英

Use of unassigned local variable c# error

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(); 
    }
}

}

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 .

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 .

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

try int moneyBet=0;

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

在声明变量moneyBet时,如果在Convert.ToInt32期间抛出异常,moneyBet将保持未分配状态,因为catch块中没有分配,因此当您到达finally块时,moneyBet将被取消分配。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM