我注意到我无法添加到索引0之后的对象列表。其他任何索引都返回空引用。
public class MultiValidation
{
public List<SingleValidation> validations { get; set; }
public MultiValidation(List<string> numArray)
{
for(int i = 0; i<numArray.Count; i++)
{
SingleValidation individual = new SingleValidation(Validate.idArray[i], Validate.actionArray[i], Validate.expiryArray[i]);
validations = new List<SingleValidation>();
validations.Add(individual);
Console.WriteLine(validations[i].action);
}
}
这里是SingleValidation
public SingleValidation(string ide, string ac, string exDate)
{
this.action = ac;
this.expiry = exDate;
this.id = ide;
}
我已经测试过idArray[i]
,actionArray[i]
,expiryArray[i]
都是字符串。
答案 0 :(得分:1)
validations = new List<SingleValidation>();
这应该在for循环之外。
{
validations = new List<SingleValidation>();
for(int i = 0; i<numArray.Count; i++)
{
SingleValidation individual = new SingleValidation(Validate.idArray[i],
Validate.actionArray[i], Validate.expiryArray[i]);
validations.Add(individual);
Console.WriteLine(validations[i].action);
}
}