Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field 'xxx' is never assigned to, and will always have its default value null

Tags:

c#

My error: Field 'StockManagement.LargeItems1.largeS' is never assigned to, and will always have its default value null My code:

namespace StockManagement
{
    class LargeItems1
    {
        private Stack<string> largeS;

         public LargeItems1()
        {
            Stack<string> largeS = new Stack<string>();
        }
        public void LargeItemsAdd()
        {
            string usersInput2, tempValue;
            int tempValueI = 0;
            bool attempt = false;//, whichOne = false;
            Console.WriteLine("Would you like to remove or add an item to the storage area \n Reply add OR remove");
            string usersInput = Console.ReadLine();
            usersInput = usersInput.ToLower();

            if (usersInput.Contains("remove"))
            {
                LargeItemsRemove(largeS);
                return;
            }
            else if (usersInput.Contains("add"))
            {

                Console.WriteLine("Please input (numerically) how many IDs you'd like to add");
                tempValue = Console.ReadLine();
                attempt = int.TryParse(tempValue, out tempValueI);
                while (!attempt)
                {
                    Console.WriteLine("Please input (numerically) how many IDs you'd like to add, you did not input a numeric value last time");
                    tempValue = Console.ReadLine();
                    attempt = int.TryParse(tempValue, out tempValueI);
                }
                for (int i = 0; i < tempValueI; i++)
                {

                    Console.WriteLine("Please input the ID's (one at a time) of the item you would like to add");
                    usersInput2 = Console.ReadLine();
                    if (largeS.Contains(usersInput2))
                    {
                        Console.WriteLine("That ID has already been stored");
                    }
                    else
                    {
                        largeS.Push(usersInput2);
                    }
                }
                foreach (var item in largeS)
                {
                    Console.WriteLine("Current (large) item ID's: " + item);
                }
            }

        }
        public void LargeItemsRemove(Stack<string> largeS)
        {
            if (largeS.Contains(null))
            {
                Console.WriteLine("No IDs stored");
            }
            else
            {

                string popped = largeS.Pop();
                foreach (var item in largeS)
                {
                    Console.WriteLine("Pop: " + item);
                }
                Console.WriteLine("Removed ID = " + popped);
            }
        }

    }
}

I don't understand how to assign my values to the instances. I'd appreciate any help that can be provided!

like image 964
Zain Avatar asked Jan 23 '26 22:01

Zain


1 Answers

Change your constructor to initialize the field instead of the local variable:

public LargeItems1()
{
    this.largeS = new Stack<string>();
}
like image 152
Tim Schmelter Avatar answered Jan 25 '26 12:01

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!