Results 1 to 4 of 4

Thread: Use of unassigned local variable?

  1. #1
    JungleHeart is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Use of unassigned local variable?

    Greetings,

    I am completely new to programming and also to C#. I'm working out of a book called "Beginning C# 6 Programming with Visual Studio 2015". There is an example that I'm having trouble de-bugging and I swear I have typed everything exactly as in the book! but perhaps I'm missing something... I keep getting the error : Use of unassigned local variable 'firstNumber'
    here is the code:


    {


    double firstNumber, secondNumber;
    string userName;
    Console.WriteLine("Enter your name:");
    userName = Console.ReadLine();
    Console.WriteLine($"Welcome {userName}!");
    Console.WriteLine("Now give me a number:");
    secondNumber = Convert.ToDouble(Console.ReadLine());
    Console.WriteLine($"The sum of {firstNumber} and {secondNumber} is " +
    $"{firstNumber + secondNumber}.");
    Console.WriteLine($"The result of subtracting {secondNumber} from " +
    $"{firstNumber} is {firstNumber - secondNumber}.");
    Console.WriteLine($"The result of dividing {firstNumber} by " +
    $"{secondNumber} is {firstNumber / secondNumber}.");
    Console.WriteLine($"The remainder after dividing {firstNumber} by " +
    $"{secondNumber} is {firstNumber % secondNumber}.");
    Console.ReadKey();


    }


    Can someone help??? Thanks in advance!!!
    Last edited by JohnH; 03-17-2017 at 3:53 AM. Reason: xcode box

  2. #2
    JohnH's Avatar
    JohnH is offline C# Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Location
    Norway
    Posts
    333
    Rep Power
    88
    You use firstNumber without having assigned something to it beforehand. You can assign when declaring it:
    double firstNumber = 0;

  3. #3
    JungleHeart is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    thanks!

    Quote Originally Posted by JohnH View Post
    You use firstNumber without having assigned something to it beforehand. You can assign when declaring it:
    double firstNumber = 0;
    Excellent, Thanks!
    I'll let the authors of the book know about the mistake...

  4. #4
    VitzzViperzz is offline C# Forum Enthusiast
    .NET Framework
    .NET 4.5
    Join Date
    Jan 2017
    Posts
    34
    Rep Power
    6
    How are you finding the book so far?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •