不包含一个构造函数4个参数? [英] Does not contain a constructor that takes 4 arguments?

查看:210
本文介绍了不包含一个构造函数4个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的节目,我一直停留在这个问题上一段时间,现在,我已经寻找的答案,在整个互联网这个问题,但我仍然难倒,为什么它不工作。编译器说,低于code不包含一个构造函数4个参数?我不明白,为什么?

在code是:

 公共类用户
{
    私人诠释_ID;
    私人字符串_FName;
    私人字符串_LName;
    私人字符串_address;
    私人字符串_Phone;

    公众诠释ID
    {
        {返回_ID; }
    }

    公共字符串FName参数
    {
        {返回_FName; }
    }

    公共字符串LName的
    {
        {返回_LName; }
    }

    公共字符串地址
    {
        {返回_address; }
    }

    公共字符串电话
    {
        {返回_Phone; }
    }
}
 

这是有问题的code是:

 公共静态无效的插入(字符串FName参数,字符串LName的,字符串地址,串电话)
{
    用户新用户=新用户(FName参数,LName的,地址,电话);
    newUser.Save();
}
 

解决方案

声明一个构造函数4个参数:

 类用户
{
    公众用户(串名字,字符串姓氏,字符串的地址,串电话)
    {
       _fName =名字;
       ....
    }
}
 

用法:

 用户的用户=新用户(乔,...);
 


或增加公共二传手类的属性,然后使用对象初始化:

 公共字符串名字{获得;组; } //通知公众
 

用法:

 用户的用户=新用户{名字=乔,...};
 

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? 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:

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", ... };

这篇关于不包含一个构造函数4个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆