简体   繁体   English

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

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

My error: Field 'StockManagement.LargeItems1.largeS' is never assigned to, and will always have its default value null My code: 我的错误: 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);
            }
        }

    }
}

I don't understand how to assign my values to the instances. 我不明白如何将我的值分配给实例。 I'd appreciate any help that can be provided! 我很感激可以提供任何帮助!

Change your constructor to initialize the field instead of the local variable: 更改构造函数以初始化字段而不是局部变量:

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

You have to initialize your field largeS , using the this keyword, in this way: 您必须使用this关键字以这种方式初始化字段largeS

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

The this keyword is used to refer to the current instance of the class. this关键字用于引用类的当前实例。 If you use instead: 如果你改用:

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

you're just initializing a new local variable that has nothing to do with your private field. 您只是初始化一个与您的私有字段无关的新本地变量。

Check the MSDN documentation about this keyword. 查看有关关键字的MSDN文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 永远不会将字段xxx分配给,并始终将其默认值设置为null - Field xxx is never assigned to, and will always have its default value null 字段&#39;XXX.AppMain.p1&#39;从未分配,并且其默认值始终为空 - Field 'XXX.AppMain.p1' is Never Assigned to, And Will Always Have Its Default Value Null 字段…永远不会分配给它,并且其默认值始终为null - Field … is never assigned to, and will always have its default value null 该字段永远不会分配给它,并且其默认值始终为null - The Field Is Never Assigned To And Will Always Have Its Default Value null 字段从未分配给它,并且将始终具有其默认值null - Field is never assigned to, and will always have its default value null 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 私有类中的公共字段:“Field [FieldName]永远不会分配给,并且将始终具有其默认值null” - Public field in private class: “Field [FieldName] is never assigned to, and will always have its default value null” 警告“字段 .... 从未分配给并且将始终具有默认值 null” - Warning "Fields .... is never assigned to and will always have its default value null" 字段modifyDate永远不会分配给,并且始终具有其默认值0 - Field modifyDate is never assigned to, and will always have its default value 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM