gpt4 book ai didi

c# - 字段 'xxx' 从未分配给,并且始终具有默认值 null

转载 作者:行者123 更新时间:2023-11-30 13:22:29 29 4
gpt4 key购买 nike

我的错误: 字段“StockManagement.LargeItems1.largeS”从未分配给,并且始终具有默认值 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);
}
}

}
}

我不明白如何将我的值分配给实例。如果能提供任何帮助,我将不胜感激!

最佳答案

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

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

关于c# - 字段 'xxx' 从未分配给,并且始终具有默认值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21626767/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com