我收到错误:没有给出对应于'CsvLines.CsvLines(字符串,字符串,字符串,字符串,字符串)'所需的形式参数'value'的参数
以下是我将值添加到班级的地方。
List<CsvLines> valuesList = new List<CsvLines>();
Console.WriteLine("Currently filtering " + fileName);
while ((line = streamReader.ReadLine()) != null)
{
// Change the logic here depending on what we want from the file. Depending on the list it might be better to filter by things we want.
if (line.Contains("|")) //Add variable here to search for in the files
{
string[] csvFields = line.Split('|');
CsvLines lines = new CsvLines
{
Value = csvFields[0],
Name = csvFields[1],
Spanish = csvFields[2],
French = csvFields[3],
Russian = csvFields[4]
};
valuesList.Add(new CsvLines(lines.Value, lines.Name, lines.Spanish, lines.French, lines.Russian));
}
fileRowCount++;
}
以下是我的实际课程:
namespace LogHarvester
{
public class CsvLines
{
public string Value { set; get; }
public string Name { set; get; }
public string Spanish { set; get; }
public string French { set; get; }
public string Russian { set; get; }
public CsvLines(string value, string name, string spanish, string french, string russian)
{
this.Value = value;
this.Name = name;
this.Spanish = spanish;
this.French = french;
this.Russian = russian;
}
}
}
我会给你更多的信息,但我已经做了一些搜索,老实说我不知道,很多搜索返回我可能需要一个基础构造函数,但我不确定这将如何解决问题。
我打算解析每一行然后将它插入一个数组,然后将数组位置分配给值,然后从那里我将它们添加到所述类“CsvLines”的列表中。任何帮助将不胜感激,如果您需要更多信息,我会尽力给您。
答案 0 :(得分:0)
问题出在这个声明中:
CsvLines lines = new CsvLines
{
Value = csvFields[0],
Name = csvFields[1],
Spanish = csvFields[2],
French = csvFields[3],
Russian = csvFields[4]
};
这样你实际上是在没有参数的情况下调用构造函数(例如new CsvLines()
)。您应该在类中定义默认构造函数。
或者,不需要创建两个新的CsvLines
对象(或定义默认构造函数),只需:
valuesList.Add(new CsvLines(csvFields[0], csvFields[1], csvFields[2], csvFields[3], csvFields[4]));