List.Add()问题仅保存最后添加的项目 [英] Issue with List.Add() only saving the last added item

查看:81
本文介绍了List.Add()问题仅保存最后添加的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到的问题是以下代码行:

The issue I'm noticing is this line of code:

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 add the correct item to the list but if I hover over tempList and want to see its contents, although it has two items, both of them are the same - they are both now the second item that was added to the list. It has overwritten the first one.

我不知道这是怎么回事以及为什么会发生。

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

推荐答案

您需要在for循环中实例化 Orderables 。否则,您将在所有迭代中重复使用同一实例(并每次都覆盖其属性)。

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);
}

与该问题无关:您可能会找到对象初始值设定项语法更简洁:

Unrelated to the question: You might find object initializer syntax to be cleaner:

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天全站免登陆