2012-06-23 42 views
3

因此,在这一刻,我有一个多维数组我应该使用列表吗?

string[,] items = new string[100, 4]; 

然后我收集需要的投入将它们放到数组,然后在列表框中显示它们

items[itemcount, 0] = id; 
items[itemcount, 1] = newprice; 
items[itemcount, 2] = quant; 
items[itemcount, 3] = desc; 

listBox1.Items.Add(items[itemcount, 0] + "\t" + items[itemcount, 3] + "\t " + items[itemcount, 2] + "\t " + items[itemcount, 1]); 
listBox1.SelectedIndex = listBox1.Items.Count - 1; 

所以这时用户可以如果他们想从列表框中选择一个项目来删除该项目。当删除一个项目时,我意识到一个数组不适合。因此,我应该创建4个不同的列表,并使用list.Remove方法删除它们,或者有更好的方法,我不必在4种不同的东西上工作,用户使用WinXP运行一台旧电脑,我将不得不担心性能与4个不同的名单?有什么比如多维列表吗?

堆感谢您的帮助

+0

试着看'字典' –

回答

7

您正在尝试重新创建类的实例列表。像

class Item { 
    public int Id {get;set;} 
    public double Price {get;set;} 
    public double Quantity {get;set;} 
    public string Description {get;set;} 
} 

var myItems = new List<Item>(); 
+1

这是名单如何能帮助你在这种情况下,一个很好的例子。 –

+0

考虑将价格更改为额外信用的“小数”。 – recursive

+0

好吧,只有一个问题,他们索引?我能去myItems [2]或类似的东西来获得第二个项目?然后当删除一个我循环每一个找到它是哪个索引,然后去myItems [2] .Remove()什么的? – MosesIAmnt

3

随着你与你真的要工作应该看不到任何性能问题的数据量。

任何类型的集合,无论是List<T>,Dictionary<T, T>,IEnumerable<T>或任何你想使用的,它都会为你提供比数组更多的功能。

2

它看起来像你有一个复杂的类型,你应该把一个列表。不知道名称是否正确,但是像这样的东西看起来像你想要的。

public class InventoryEntry 
{ 
    public string Id {get;set;} 
    public double NewPrice {get;set;} 
    public int Quantity {get;set;} 
    public string Description {get;set;} 

    public override ToString() 
    { 
      //return your specially formatted string here 
    } 
} 
var items = new List<InventoryEntry>(); 
//add some items 
//add them to your listbox 
//etc...