【问题标题】:No argument given for formal parameter没有为形式参数提供参数
【发布时间】:2016-05-24 10:29:59
【问题描述】:

我收到错误:没有给出与“CsvLines.CsvLines(string, string, string, string, string)”所需的形参“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”的列表中。任何帮助将不胜感激,如果您需要更多信息,我会尽力提供给您。

【问题讨论】:

    标签: c# class console


    【解决方案1】:

    问题在于这个声明:

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

    【讨论】:

    • 谢谢,非常有帮助,我没有意识到如果你要在运行时初始化它们,你需要一个空的构造函数。感谢您清除它。
    猜你喜欢
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多