问题与List.Add():只保存最后添加的项目 [英] Issue with List.Add(): it only saves the last added item

查看:165
本文介绍了问题与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并希望看到它的内容,但它有两个项目,但两者是相同的,他们都是现在被添加到第二项列表,以便它已经覆盖了第一个,...

when I put my breakpoint on the line mentioned above ( tempList.Add(orderables); ) the first time it adds the item correctly to the templist and it will have one item in it. The second time it will the correct item to the list BUT if I hover over tempList and want to see its contents, although it has two items but both of them are the same and they are both now the second item that is being added to the list so it has overwritten the first one,...

我想不出这有什么走错了,为什么它正在发生。

I can't figure out what is going wrong with this and why it is happening.

感谢所有。

推荐答案

您需要实例化 Orderables for循环;否则,你把所有的迭代重复使用相同的实例(和覆盖每一次它的属性)。

You need to instantiate the Orderables within the for loop; otherwise, you keep reusing the same instance in all iterations (and overwriting its properties each time).

AssociatedComboItems ai = new AssociatedComboItems();
List<Orderables> tempList = new List<Orderables>();

foreach (var t in comboBox1.Items)
{
    ai.ComboBoxItem = t.ToString();

    for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
    {
        Orderables orderables = new Orderables();  // ← Instantiate here
        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);
}



无关的问题:您可能会发现的对象初始化语法是清洁的:

        Orderables orderables = new Orderables
        {
            Display = fpSpread1.ActiveSheet.Cells[i, 1].Text,
            ShowInDSR = (bool)fpSpread1.ActiveSheet.Cells[i, 0].Value,
            DisplayOrder = i,
        };

这篇关于问题与List.Add():只保存最后添加的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆