简体   繁体   English

不包含带有4个参数的构造函数?

[英]Does not contain a constructor that takes 4 arguments?

I'm fairly new to programming and I've been stuck on this question for awhile now, I have searched for an answer to this question throughout the internet but am still stumped as to why it is not working. 我对编程还很陌生,现在已经在这个问题上停留了一段时间,我在整个互联网上都在寻找该问题的答案,但是对于为什么它不起作用仍然感到困惑。 The compiler says that the code below does not contain a constructor that takes 4 arguments? 编译器说下面的代码不包含带有4个参数的构造函数? I don't understand why? 我不明白为什么?

The code is: 代码是:

public class Users
{
    private int _ID;
    private string _FName;
    private string _LName;
    private string _Address;
    private string _Phone;

    public int ID
    {
        get { return _ID; }
    }

    public string FName
    {
        get { return _FName; }
    }

    public string LName
    {
        get { return _LName; }
    }

    public string Address
    {
        get { return _Address; }
    }

    public string Phone
    {
        get { return _Phone; }
    }
}

The code that is having trouble is: 有问题的代码是:

public static void Insert(string FName, string LName, string Address, string Phone)
{
    Users newUser = new Users(FName, LName, Address, Phone);
    newUser.Save();
}

Declare a constructor that takes 4 arguments: 声明一个带有4个参数的构造函数:

class User
{
    public User(string firstName, string lastName, string address, string phone)
    {
       _fName = firstName;
       ....
    }
}

Usage: 用法:

User user = new User("Joe", ...);

or add public setter to class properties, then use object initializer: 或在类属性中添加公共设置器,然后使用对象初始化器:

public string FirstName { get; set; } // notice public

Usage: 用法:

User user = new User { FirstName = "Joe", ... };

When you write new Users(...) you are calling a constructor of the Users class. 当您编写new Users(...)您正在调用Users类的构造函数。 Since you haven't defined one, the default one takes zero arguments, not four as you have used. 由于您尚未定义一个,因此默认值将接受零个参数,而不是您所使用的四个。

You could use the default constructor and set the properties with an object initializer. 您可以使用默认构造函数,并使用对象初始化程序设置属性。 To do that, replace this: 为此,请替换为:

Users newUser = new Users(FName, LName, Address, Phone);

with this: 有了这个:

Users newUser = new Users() { FName = FName, LName = LName, Address = Address, Phone = Phone };

Or, you could just add a constructor that takes four arguments, like this: 或者,您可以添加一个带有四个参数的构造函数,如下所示:

public Users(string fName, string lName, string address, string phone)
{
    FName = fName;
    LName = lName;
    Address = address;
    Phone = phone;
}

I'm not very experienced with C#, but your class doesn't seem to have a constructor declared. 我对C#不太熟悉,但是您的类似乎没有声明构造函数。 You're declaring the variables FName, LName, Address and Phone, but they never get any value. 您在声明变量FName,LName,Address和Phone,但它们从未获得任何值。

You might try adding this to the class Users, below your declarations: 您可以尝试将其添加到声明下面的Users类中:

public Users(String fname, String lname, String address, String phone)
{
    FName = fname;
    LName = lname;
    Address = address;
    Phone = phone;
}

You might also consider changing the name of 'Users' to just 'User', since each instantiation of the class represents a single User, not a collection of them. 您可能还考虑将“用户”的名称更改为“用户”,因为类的每个实例都代表一个用户,而不是它们的集合。

You need a constructor. 您需要一个构造函数。 It is not given. 没有给出。 It has to be written. 必须写出来。

I've seen some people trying to give a constructor an amount of parameters the same as the amount of fields it has. 我见过有人试图给构造函数提供与其数量相同的参数。 But if the constructor exists already in all likelihood all you need to do is: 但是,如果构造函数很可能已经存在,那么您需要做的就是:

Users user1 = new Users();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM