2

請求書タイプの新しいオブジェクトを作成しようとしています。必要なパラメーターを正しい順序で渡します。

ただし、無効な引数が含まれていることがわかります。ここで非常に単純なことを見落としているかもしれませんが、誰かが指摘できるかもしれません。

私は宿題に取り組んでいますが、プロジェクトで使用するために 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
4

1 に答える 1

4

あなたのメソッドは、値(57.98)decimalを渡すときにパラメーターを期待しています。double

MSDNごとに (備考セクションを参照)、

浮動小数点型と 10 進数型の間に暗黙的な変換はありません。

小数の場合は、接尾辞「m」または「M」を追加する必要があります

したがって、あなたの場合、57.98 の代わりに 57.98m を渡します。

この SO 回答には、すべての種類のサフィックスがリストされています。

于 2012-10-05T03:55:47.067 に答える