将问题添加到自定义类类型的列表中

时间:2018-08-28 13:56:02

标签: c# ios sqlite xamarin.ios

对于某人来说,这很可能是一个非常基本的问题,但我无法解决。基本上,我有一个用于列表的自定义类:

public class CorrectiveMaintResults
{
    public string Comment { get; set; }
    public string Cleared { get; set; }
}

我最终将其存储在数据库中,在注释和清除的每个项目之间放置一个换行符。然后,我将其检索回表中。为了实现这一点,我创建了两个私有列表,这些列表接受注释并从数据库中清除了数据。

我想做的是创建一个for或foreach循环,将这两个私有列表重新添加到我的自定义列表(correctiveMainResults)中。这就是我正在尝试的:

for (int i = 0; i < correctiveMainComment.Count; i++)
{
    maintResults.Comment = correctiveMainComment[i];
    maintResults.Cleared = correctiveMainCleared[i];

    FormResults.correctiveMainResults.Add(maintResults);
}

mainResults是我对该类的初始化:

CorrectiveMaintResults maintResults = new CorrectiveMaintResults();

我遇到的问题是,当将结果添加到FormResults.correctiveMainResults中时,它似乎只出现在每个列表的最后一个索引上,而只是对其进行重复。

希望有人可以帮助或理解我的意思。在没有实际看到应用程序运行的情况下很难解释。

1 个答案:

答案 0 :(得分:6)

您仅在循环外创建一个对象。尝试这样的事情:

for (int i = 0; i < correctiveMainComment.Count; i++) {
    CorrectiveMaintResults maintResults = new CorrectiveMaintResults();
    maintResults.Comment = correctiveMainComment[i];
    maintResults.Cleared = correctiveMainCleared[i];

    FormResults.correctiveMainResults.Add(maintResults);
}