令人沮丧的错误(面向对象的编程)

时间:2015-10-13 05:04:05

标签: c#

偶然发现了一些编码我所拥有的任务的错误,似乎无法解决。有人有想法吗?

我一直在接受

  

错误CS1061' clsCustomer'不包含' Street'的定义   并没有延伸方法' Street'接受第一个类型的参数   ' clsCustomer'可以找到(你是否错过了使用指令或   装配参考?)

     

错误CS1729' clsCustomer'不包含5的构造函数   参数。

     

错误CS7036没有给出对应的参数   所需的形式参数' strState' of' clsOrder.clsOrder(string,   string,string,string,string,int,decimal)'

这是代码

class clsOrder
{
    //declare class variables include shared (static)
    protected string cstrName;
    protected string cstrStreet;
    protected string cstrCity;
    protected string cstrState;
    protected string cstrZip;

    protected int cintQuantity;
    protected decimal cdecPrice;


    protected static decimal cdecExtendedPrice;
    protected static int cintTotalCount;
    protected static decimal cdecTotalPrice;



//declare constructors
public clsOrder()
    {

    }
    public clsOrder(string strName, string strStreet, string strCity, string strState, string strZip, int intQuantity, decimal decPrice)
    {
        this.Name = strName;
        this.Street = strStreet;
        this.City = strCity;
        this.State = strState;
        this.Zip = strZip;
        this.Quantity = intQuantity;
        this.Price = decPrice;
    }


//declare property methods
public string Name
    {
        get
        {
            return cstrName;
        }
        set
        {
            cstrName = value;
        }
    }

    public string Street
    {
        get
        {
            return cstrStreet;
        }
        set
        {
            cstrStreet = value;
        }
    }

    public string City
    {
        get
        {
            return cstrCity;
        }
        set
        {
            cstrCity = value;
        }
    }

    public string State
    {
        get
        {
            return cstrState;
        }
        set
        {
            cstrState = value;
        }
    }

    public string Zip
    {
        get
        {
            return cstrZip;
        }
        set
        {
            cstrZip = value;
        }
    }

    public int Quantity
    {
        get
        {
            return cintQuantity;
        }
        set
        {
            cintQuantity = value;
        }
    }

    public decimal Price
    {
        get
        {
            return cdecPrice;
        }
        set
        {
            cdecPrice = value;
        }
    }

这是我得到错误的地方

clsCustomer cobjCustomer = new clsCustomer(txtName.Text, txtStreet.Text,
                    txtCity.Text, txtState.Text, txtZip.Text); //Here

strMailingLabel = cobjCustomer.Name + "\n" +     //Here     
                  cobjCustomer.Street + "\n" +    //Here
                  cobjCustomer.City + ", " +     //Here
                  cobjCustomer.State + "  " + cobjCustomer.Zip;    //Here

lblMailingLabel.Text = strMailingLabel;

clsOrder cobjOrder = new clsOrder  //Here
    (txtDescription.Text,
     int.Parse(txtQuantity.Text),
     decimal.Parse(txtPrice.Text));

3 个答案:

答案 0 :(得分:1)

您已声明名为clsOrder的课程 并且您创建了类clsCustomer

的实例

相应地改变:

clsOrder cobjCustomer = new clsOrder(txtName.Text, txtStreet.Text,
                                txtCity.Text, txtState.Text, txtZip.Text); //Here


            strMailingLabel = cobjCustomer.Name + "\n" +     //Here     
                              cobjCustomer.Street + "\n" +    //Here
                              cobjCustomer.City + ", " +     //Here
                              cobjCustomer.State + "  " + cobjCustomer.Zip;    //Here


            lblMailingLabel.Text = strMailingLabel;


            clsOrder cobjOrder = new clsOrder  //Here
                (txtDescription.Text,
                 int.Parse(txtQuantity.Text),
                 decimal.Parse(txtPrice.Text));

如果它们不重要,你需要将最后两个参数声明为可选:

public clsOrder(string strName, string strStreet, string strCity, string strState, 
                string strZip, int intQuantity = 0, decimal decPrice = 0)

答案 1 :(得分:0)

在构造函数签名中,您声明期望int intQuantity, decimal decPrice,但您只提供地址字段。如果您希望数量和价格是可选的,则需要提供默认值:

public clsOrder(string strName, string strStreet, string strCity, string strState, 
                string strZip, int intQuantity = 0, decimal decPrice = 0)

此外,您已向我们展示了clsOrder的代码,因此我认为这是您要实例化的内容,而不是clsCustomer

答案 2 :(得分:0)

一个你的构造函数需要7个参数,你传递的是5。

你已经将你的构造函数设置为7个参数,但是因此传递了5个关于传递5个参数的错误,而不是7个。

现在,如果不需要最后两个,您可以使用可选参数。这意味着如果您不提供它们,则默认为值。

public clsOrder(string strName, string strStreet, string strCity, string strState, string strZip, int intQuantity, decimal decPrice)
    {
        this.Name = strName;
        this.Street = strStreet;
        this.City = strCity;
        this.State = strState;
        this.Zip = strZip;
        this.Quantity = intQuantity;
        this.Price = decPrice;
    }

应该是:

 public clsOrder(string strName, string strStreet, string strCity, string strState, string strZip, int Quantity = 0, decimal decPrice = 0.0)
    {
        this.Name = strName;
        this.Street = strStreet;
        this.City = strCity;
        this.State = strState;
        this.Zip = strZip;
        this.Quantity = intQuantity;
        this.Price = decPrice;
    }