中的代碼行問題與List.Add():它僅保存最後添加的項目
tempList.Add(orderables);
在這個全碼:
AssociatedComboItems ai = new AssociatedComboItems();
List<Orderables> tempList = new List<Orderables>();
Orderables orderables = new Orderables();
foreach (var t in comboBox1.Items)
{
ai.ComboBoxItem = t.ToString();
for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
{
orderables.Display = fpSpread1.ActiveSheet.Cells[i, 1].Text;
orderables.ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value;
orderables.DisplayOrder = i;
tempList.Add(orderables);
}
ai.AssociatedItems = tempList;
tempList.Clear();
if(AssociatedItems == null)
AssociatedItems = new List<AssociatedComboItems>();
AssociatedItems.Add(ai);
}
,當我把我的斷點上線如上所述(tempList.Add(orderables);) 它第一次正確地將項目添加到templist,它將有一個項目在其中。第二次它將正確的項目列表,但如果我將鼠標懸停在tempList上,並希望查看其內容,雖然它有兩個項目,但他們都是相同的,他們現在都是第二個項目被添加到所以它已經覆蓋了第一個,...
我不明白這是什麼問題,爲什麼發生。
謝謝大家。
tempList.Clear();清空列表 –