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