构造函数错误的参数数量

时间:2012-04-04 14:11:33

标签: c# constructor arguments

首先,我很抱歉在如此复杂的地方提出这么简单的问题。我正在编写一个用于解析文本文件的一次性应用程序。它是一个具有典型静态Main()的控制台应用程序。

我已经宣布了另一个类。由于操作的性质,我省略了get / set(它将被使用一次......永远不会再次使用)。

public class Entry
{
    public List<string> numbers;
    public string rm;
    public string time;
    public string desc;

    Entry(List<string> n, string r, string t, string d)
    {
        numbers = n;
        rm = r;
        time = t;
        desc = d;
    }
}

当我尝试使用以下语句在Main()中实例化类时:

Entry newEntry = new Entry(numbers, rn, time, desc);

我收到的错误是Entry没有带4个参数的构造函数。传递的所有变量都与构造函数定义类型匹配。我睡眠不足,很困惑。我做错了什么?

谢谢,抱歉这个愚蠢的问题。

2 个答案:

答案 0 :(得分:5)

您已将构造函数设为私有。如果您没有为构造函数使用访问修饰符,则默认情况下它将设置为私有。将“public”放在构造函数的前面,它应该可以工作:

public class Entry
{
    public List<string> numbers;
    public string rm;
    public string time;
    public string desc;

    public Entry(List<string> n, string r, string t, string d)
    {
        numbers = n;
        rm = r;
        time = t;
        desc = d;
    }
}

答案 1 :(得分:2)

尝试制作构造函数public

public Entry(List<string> n, string r, string t, string d)