2012-06-23 46 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...