字段'xxx'永远不会分配给,并且将始终具有其默认值null

时间:2014-02-07 11:37:38

标签: c#

我的错误:    Field 'StockManagement.LargeItems1.largeS' is never assigned to, and will always have its default value null 我的代码:

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

    }
}

我不明白如何将我的值分配给实例。我很感激可以提供任何帮助!

2 个答案:

答案 0 :(得分:10)

更改构造函数以初始化字段而不是局部变量:

public LargeItems1()
{
    this.largeS = new Stack<string>();
}

答案 1 :(得分:3)

您必须使用largeS关键字以这种方式初始化您的字段this

this.largeS = new Stack<string>();

this关键字用于指代类的当前实例。如果您改为使用:

Stack<string> largeS = new Stack<string>();

您只是初始化一个与您的私有字段无关的新本地变量。

查看有关this关键字的MSDN文档。