Daniel Lukish
Daniel Lukish

Reputation: 125

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

}

Upvotes: 0

Views: 2121

Answers (5)

Lee
Lee

Reputation: 144136

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.

Upvotes: 1

jpmnteiro
jpmnteiro

Reputation: 777

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.

Upvotes: 0

George Chondrompilas
George Chondrompilas

Reputation: 3237

try int moneyBet=0;

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

Upvotes: 0

Tobi
Tobi

Reputation: 5519

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

Upvotes: 0

Benjamin Trent
Benjamin Trent

Reputation: 7566

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.

Upvotes: 1

Related Questions