C# 列表<;结构>;VS列表<;类别>;在C中#
我基于struct创建了一个列表,但无法更改项值。所以我用了一个类而不是struct,但问题是当我打印列表时,它会根据项目的数量给我最新插入的项目的副本 如果我插入“A”、“B”、“C”,那么输出将是C而不是A、B、C 代码如下:C# 列表<;结构>;VS列表<;类别>;在C中#,c#,asp.net,visual-studio,c#-4.0,c#-3.0,C#,Asp.net,Visual Studio,C# 4.0,C# 3.0,我基于struct创建了一个列表,但无法更改项值。所以我用了一个类而不是struct,但问题是当我打印列表时,它会根据项目的数量给我最新插入的项目的副本 如果我插入“A”、“B”、“C”,那么输出将是C而不是A、B、C 代码如下: public struct Item //this is working fine but can't change item price { public string Code { get; set; } public string Des
public struct Item //this is working fine but can't change item price
{
public string Code { get; set; }
public string Description{ get; set; }
public string Price{ get; set; }
public string Qty { get; set; }
}
代码的其余部分
public static Item xItem = new Item();
public static List<Item> item = new List<Item>();
xItem.Code = txtCode.Text;
xItem.Description = txtDescription.text;
xItem.Price= txtPrice.text;
xItem.Qty = txtQty.text;
在btnSave\u中单击
I添加此
foreach (var s in item)
{
System.Diagnostics.Debug.WriteLine(s.Code +" \t " + s.Qty);
}
对于
struct
版本。。。没有理由不能更改结构中的值。你能更全面地解释一下你在那里遇到的问题吗?我怀疑这与使用static
有关,这似乎在这里没有位置。。。但问题中没有足够的信息让我们确定
对于类
版本,每次要添加条目时,都需要创建一个新的项
实例:
//why are these static? That seems like a bad idea, but we lack enough context to know for sure
public static Item xItem;
public static List<Item> item = new List<Item>();
xItem = new Item();
xItem.Code = txtCode.Text;
xItem.Description = txtDescription.text;
xItem.Price= txtPrice.text;
xItem.Qty = txtQty.text;
//为什么这些是静态的?这似乎是个坏主意,但我们缺乏足够的背景来确定
公共静态项xItem;
公共静态列表项=新列表();
xItem=新项();
xItem.Code=txtCode.Text;
xItem.Description=txtDescription.text;
xItem.Price=txtPrice.text;
xItem.Qty=txtQty.text;
这将允许您向列表中添加多个类实例。。。但是如果我的怀疑是正确的,那么您将处于与使用
struct
时相同的情况,并且我发现static
很可能是罪魁祸首。对于struct
版本。。。没有理由不能更改结构中的值。你能更全面地解释一下你在那里遇到的问题吗?我怀疑这与使用static
有关,这似乎在这里没有位置。。。但问题中没有足够的信息让我们确定
对于类
版本,每次要添加条目时,都需要创建一个新的项
实例:
//why are these static? That seems like a bad idea, but we lack enough context to know for sure
public static Item xItem;
public static List<Item> item = new List<Item>();
xItem = new Item();
xItem.Code = txtCode.Text;
xItem.Description = txtDescription.text;
xItem.Price= txtPrice.text;
xItem.Qty = txtQty.text;
//为什么这些是静态的?这似乎是个坏主意,但我们缺乏足够的背景来确定
公共静态项xItem;
公共静态列表项=新列表();
xItem=新项();
xItem.Code=txtCode.Text;
xItem.Description=txtDescription.text;
xItem.Price=txtPrice.text;
xItem.Qty=txtQty.text;
这将允许您向列表中添加多个类实例。。。但是如果我的怀疑是正确的,那么您将处于与使用struct
时相同的情况,并且我发现static
很可能是罪魁祸首。这是由于。引用类型(=类)只是指向实例的指针。因此,当您将一个对象传递给方法时,您实际上提供了一个指向该对象的指针,而不是实际对象。通过该引用更改的所有内容都会反映在对该实例的所有引用上
在您的例子中,您只有一个用于不同实例语义的引用。因此,创建一个新实例,而不是重复使用现有实例:
public static Item xItem = new Item();
public static List<Item> item = new List<Item>();
作为备用结构,结构通常应该是不可变的。因此,无论何时想要修改实例状态,都应该考虑使用类而不是结构。要进一步了解Immutably,您也可以阅读这篇文章:这是由于。引用类型(=类)只是指向实例的指针。因此,当您将一个对象传递给方法时,您实际上提供了一个指向该对象的指针,而不是实际对象。通过该引用更改的所有内容都会反映在对该实例的所有引用上
在您的例子中,您只有一个用于不同实例语义的引用。因此,创建一个新实例,而不是重复使用现有实例:
public static Item xItem = new Item();
public static List<Item> item = new List<Item>();
作为备用结构,结构通常应该是不可变的。因此,无论何时想要修改实例状态,都应该考虑使用类而不是结构。要进一步了解Immutably,您也可以阅读这篇文章:您正在创建
项的静态实例,并将其命名为xItem
然后设置xItem
的属性,并将其添加到列表中-项
item
现在包含1项-xItem
然后再次设置xItem
的属性,并再次添加到列表中。
这是陷阱。您的列表现在包含2个项目-2个对xItem
的引用-不是2个不同的对象,而是同一个对象,两次
。。。又是第三次
您的代码需要更改为:
public static List<Item> item = new List<Item>();
//repeat this for each instance / 'version' of `Item` you want to add to your list.
// note the `new` keyword
var xItem = new Item();
xItem.Code = txtCode.Text;
xItem.Description = txtDescription.text;
xItem.Price= txtPrice.text;
xItem.Qty = txtQty.text;
item.Add(xItem);
公共静态列表项=新列表();
//对要添加到列表中的“Item”的每个实例/版本重复此操作。
//注意“new”关键字
var xItem=新项();
xItem.Code=txtCode.Text;
xItem.Description=txtDescription.text;
xItem.Price=txtPrice.text;
xItem.Qty=txtQty.text;
项目.添加(xItem);
您正在创建项的静态实例,并将其称为xItem
然后设置xItem
的属性,并将其添加到列表中-项
item
现在包含1项-xItem
然后再次设置xItem
的属性,并再次添加到列表中。
这是陷阱。您的列表现在包含2个项目-2个对xItem
的引用-不是2个不同的对象,而是同一个对象,两次
。。。又是第三次
您的代码需要更改为:
public static List<Item> item = new List<Item>();
//repeat this for each instance / 'version' of `Item` you want to add to your list.
// note the `new` keyword
var xItem = new Item();
xItem.Code = txtCode.Text;
xItem.Description = txtDescription.text;
xItem.Price= txtPrice.text;
xItem.Qty = txtQty.text;
item.Add(xItem);
公共静态列表项=新列表();
//对要添加到列表中的“Item”的每个实例/版本重复此操作。
//注意“new”关键字
var xItem=新项();
xItem.Code=txtCode.Text;
xItem.Description=txtDescription.text;
xItem.Price=txtPrice.text;
xItem.Qty=txtQty.text;
项目.添加(xItem);
听起来像是在使用xItem
对象。
您需要为列表中的每个项目创建一个新项目。该列表只是对象引用的列表,目前它们都指向同一个实际对象。
e、 g.本守则:
public static Item xItem = new Item();
public static List<Item> list = new List<Item>();
xItem.Code = "A";
xItem.Description = "A";
xItem.Price= "1";
xItem.Qty = "1";
list.Add(xItem);
//list now has 'A' with value of 1 in it..
xItem.Code = "B"
//without any further change, list will now have the same
//item, so its Code will now be "B":
//this will be TRUE:
var listIsNowB = (list[0].Code == "B");
听起来你是
xItem.Code = "A";
xItem.Description = "A";
xItem.Price= "1";
xItem.Qty = "1";
list.Add(xItem);
//we're now done with that *instance* of Item, so we now create a *new* one.
//we can re-use our variable without problem though.
xItem = new Item();
xItem.Code = "B";
xItem.Description = "B";
xItem.Price= "2";
xItem.Qty = "2";
//xItem is a new object, so this will work as you expect now.
list.Add(xItem);
item.Add(new Item
{
Code = txtCode.Text,
...
});