0

I am a first-timer learning C# and I just started writing my first program. I am currently running into a snag in one of my while loops. Normally I find that outside the loop my variables work like they should, but for some reason Visual Studio is giving me a warning saying that "The variable 'itemPrice' is assigned but its value is never used."

How I can I get that warning to go away and avoid any kind of bad practices?

using System;

class Program
{
    public static void Main()
    {

        // "whoa" <-- string literal
        // 43 <-- int literal
        // 43.34 <-- double literal
        // false <-- bool literal
        // 43.4f <-- float literal
        // 43f <-- float literal
        // 43m <-- decimal literal

        bool hasEnteredMoney = false;
        decimal money = 0;
        int menuSelection;
        decimal itemPrice;
        bool isValidItem;
        string itemName;

        while (!hasEnteredMoney)
        {
            Console.Write("Please enter how much money you have: ");
            hasEnteredMoney = decimal.TryParse(Console.ReadLine(), out money);

            if (!hasEnteredMoney)
            {
                Console.WriteLine("Please enter a valid decimal value.");
            }
        }

        while (money >= 3)
        {
            Console.WriteLine("You have $" + money + " left.");
            Console.WriteLine("Please enter your selection:");
            Console.WriteLine("[0] - Poke Ball - $200");
            Console.WriteLine("[1] - Great Ball - $700");
            Console.WriteLine("[2] - Ultra Ball - $1200");
            Console.WriteLine("[3] - Potion - $300");
            Console.WriteLine("[4] - Antidote - $100");
            Console.WriteLine("[5] - Clear Mail - $50");
            Console.WriteLine(": ");

            if (int.TryParse(Console.ReadLine(), out menuSelection))
            {

                isValidItem = false;

                if (menuSelection == 0)
                {
                    itemPrice = 200;
                    itemName = "Poke Ball";
                    isValidItem = true;
                }

                if (menuSelection == 1)
                {
                    itemPrice = 700;
                    itemName = "Great Ball";
                    isValidItem = true;
                }

                if (menuSelection == 2)
                {
                    itemPrice = 1200;
                    itemName = "Ultra Ball";
                    isValidItem = true;
                }

                if (menuSelection == 3)
                {
                    itemPrice = 300;
                    itemName = "Potion";
                    isValidItem = true;
                }

                if (menuSelection == 4)
                {
                    itemPrice = 100;
                    itemName = "Antidote";
                    isValidItem = true;
                }

                if (menuSelection == 5)
                {
                    itemPrice = 50;
                    itemName = "Clear Mail";
                    isValidItem = true;
                }
            }
            else
            {
                Console.WriteLine("Invalid input.");
            }

        }

        Console.WriteLine("... you ran out of money :(");

        Console.ReadKey();

    }
}
HaveSpacesuit
  • 3,572
  • 6
  • 40
  • 59
Dylan rockin
  • 9
  • 2
  • 7

3 Answers3

1

The variables itemPrice, isValidItem, itemName are being set (being assigned to), but the variables are not being used for other purposes later in the program.

The compiler is warning you that although you are setting itemPrice, isValidItem, and itemName, setting these variables is not doing anything in the program - E.G. when the program is executed, the variables will not have any effect on the operation of the program.

A B
  • 4,068
  • 1
  • 20
  • 23
0

It is giving you this error because your variables are never use in the code.

itemPrice = 200;
itemName = "Poke Ball";
isValidItem = true;

So visual studio intellisense is basicly saying : you putting all those values in your variables, but are never using any of them no where.

If you Console.WriteLine(itemName); at the end of you code, the warning should go away for all the itemName in you loop.

tl;dr You set; them but never get; them

McWut
  • 1
  • 1
-1

The warning is a good indication that you should actually do something with the value. In your case, I think you would want to do something like

money -= itemPrice;

First, it will get rid of the warning.

Second, it will keep the while loop from running indefinitely :)

joelc
  • 2,687
  • 5
  • 40
  • 60