【问题标题】:the best overloaded match for object.object has some invalid argumentsobject.object 的最佳重载匹配有一些无效参数
【发布时间】:2012-10-05 03:52:36
【问题描述】:

我正在尝试创建一个发票类型的新对象。我以正确的顺序传入必要的参数。

但是,它告诉我我包含了无效的参数。我可能在这里忽略了一些非常简单的事情,但也许有人可以指出来。

我正在做作业,但包含 Invoice.cs 文件以供项目使用。

我正在寻求的唯一解决方案是为什么我的对象不接受这些值。我以前从未遇到过对象问题。

这是我的代码:

static void Main(string[] args)
{
    Invoice myInvoice = new Invoice(83, "Electric sander", 7, 57.98);
}

这是实际的 Invoice.cs 文件:

// Exercise 9.3 Solution: Invoice.cs
// Invoice class.
public class Invoice
{
   // declare variables for Invoice object
   private int quantityValue;
   private decimal priceValue;

   // auto-implemented property PartNumber
   public int PartNumber { get; set; }

   // auto-implemented property PartDescription
   public string PartDescription { get; set; }

   // four-argument constructor
   public Invoice( int part, string description,
      int count, decimal pricePerItem )
   {
      PartNumber = part;
      PartDescription = description;
      Quantity = count;
      Price = pricePerItem;
   } // end constructor

   // property for quantityValue; ensures value is positive
   public int Quantity
   {
      get
      {
         return quantityValue;
      } // end get
      set
      {
         if ( value > 0 ) // determine whether quantity is positive
            quantityValue = value; // valid quantity assigned
      } // end set
   } // end property Quantity

   // property for pricePerItemValue; ensures value is positive
   public decimal Price
   {
      get
      {
         return priceValue;
      } // end get
      set
      {
         if ( value >= 0M ) // determine whether price is non-negative
            priceValue = value; // valid price assigned
      } // end set
   } // end property Price

   // return string containing the fields in the Invoice in a nice format
   public override string ToString()
   {
      // left justify each field, and give large enough spaces so
      // all the columns line up
      return string.Format( "{0,-5} {1,-20} {2,-5} {3,6:C}",
         PartNumber, PartDescription, Quantity, Price );
   } // end method ToString
} // end class Invoice

【问题讨论】:

  • 尝试在 57.98 之后加上“M”。事实上,我相信这个数字是一个双精度常数,不能转换为十进制,但 57.98M 是一个十进制常数。

标签: c# class object properties


【解决方案1】:

您的方法需要一个 decimal 参数,而您正在传递一个 double 值 (57.98)。

MSDN(见备注部分),

浮点类型和 十进制类型。

对于小数,您应该添加后缀“m”或“M”

所以,在你的情况下,通过 57.98m 而不是 57.98

This SO answer 列出各种后缀。

【讨论】:

  • 啊。谢谢!我应该停止熬夜编程!
猜你喜欢
  • 2013-07-27
  • 2015-10-14
  • 2018-07-14
  • 2013-01-07
  • 2014-05-17
  • 1970-01-01
  • 2014-04-14
  • 2012-12-10
相关资源
最近更新 更多