[英]How to create and use a list of items that are instances of a class
I'm looking for a simple way to store and access Iteminfo
.我正在寻找一种简单的方法来存储和访问Iteminfo
。
I have a Database with a Table with the following data: ItemId
, uItemname
, Itemname
, ItemDesc
, ItemSize
;我有一个带有以下数据的表的数据库: ItemId
, uItemname
, Itemname
, ItemDesc
, ItemSize
; usually 20 records but can be more.通常有 20 条记录,但也可以更多。
The problem is I don't know how to create a structure that supports multiple Items that can easily be read.问题是我不知道如何创建一个支持多个易于阅读的项目的结构。
What I am looking for is a structure that can create Items
that are easily read and can be created within the code.我正在寻找的是一种可以创建易于阅读并且可以在代码中创建的Items
的结构。
Example:例子:
public class Item
{
public int ItemId {get; set;}
public string uItemname {get; set;}
public string Itemname {get; set;}
public string ItemDesc {get; set;}
public float ItemSize {get; set;}
}
And inside the code I want to make a new one like在代码中我想制作一个新的
Item.Add(0,"test_item","Test","TestDesc",22.5f);
And when I ask something I need a way to access it like:当我问一些事情时,我需要一种访问它的方法,例如:
foreach(Item i in Item//IDk how to access all Created)
{
if(i.Itemname == "Test")
{
print(i.uItemname +"|"+ i.ItemSize);
}
}
Check out generic collections like the List .查看像List一样的通用 collections 。
You can add items easily like this您可以像这样轻松添加项目
var listOfItems = new List<Item>();
var yourNewItem = new Item {
ItemId = 0,
uItemname = "test_item",
Itemname = "Test",
ItemDesc = "TestDesc",
ItemSize = 22.5f
};
listOfItems.Add(yourNewItem);
And iterating through the list is done via foreach遍历列表是通过 foreach 完成的
foreach (Item i in listOfItems) {
if(i.Itemname == "Test")
print(i.uItemname +"|"+ i.ItemSize);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.