我创建了以下类来组织一些我需要保存到文本文件的变量:
public class ListOfVariablesToSave : List<List<Tuple<string, int, string>>>
{
public List<Tuple<string, int, string>> control { get; set;}
public List<Tuple<string, int, string>> interaction { get; set;}
public ListOfVariablesToSave()
{
control = new List<Tuple<string, int, string>>
{
Tuple.Create("/YD/YDC/MCSTEP", 0, ""),
Tuple.Create("/YD/YDC/NCSTEP", 0, ""),
Tuple.Create("/YD/YDC/DCSTEC", 0, ""),
Tuple.Create("/YD/YDC/DCTIME", 0, ""),
Tuple.Create("/YD/YDC/ICOUTF", 0, ""),
Tuple.Create("/YD/YDC/ICSHTF", 0, "")
};
interaction = new List<Tuple<string, int, string>>
{
Tuple.Create("/YD/YDI/MICOUP", 0, ""),
Tuple.Create("/YD/YDI/NICOUP", 0, ""),
Tuple.Create("/YD/YDI/DIEZON", 0, ""),
Tuple.Create("/YD/YDI/I1IECN", 1, ""),
Tuple.Create("/YD/YDI/I1IECT", 1, ""),
Tuple.Create("/YD/YDI/MISTATE", 0, "")
};
}
}
但是,当我使用:
创建实例时ListOfVariablesToSave myListOfVariablesToSave =
new ListOfVariablesToSave();
我得到没有元素的列表(count = 0)。特别是当我尝试使用以下代码访问列表时:
foreach (List<Tuple<string, int, string>> DB in myListOfVariablesToSave)
{
foreach (Tuple<string, int, string> varToSave in DB)
{
// DO SOMETHING.
}
}
没有任何反应,因为(外部)列表为空。我错过了什么?如何访问外部列表中的列表?
答案 0 :(得分:3)
这是因为你继承了列表,但是你没有在构造函数中添加任何内容。如果您希望创建的两个项目可用,请将这两行添加到构造函数的末尾:
Add(control);
Add(interaction);