2012-10-05 37 views
2

我想創建一個新的發票類型的對象。我按照正確的順序傳遞必要的參數。object.object的最佳重載匹配有一些無效的參數

但是,它告訴我,我已經包含無效的參數。我可能在這裏忽略了一些非常簡單的事情,但也許有人可以指出。

我正在做家庭作業,但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 
+1

嘗試在57.98之後放置'M'。實際上,這個數字是一個雙重常量,我相信並且不能轉換爲十進制,但57.98M是一個十進制常量。 –

回答

4

你的方法需要另外一種decimal參數,此處爲你傳遞一個double值(57.98)。

MSDN(見註釋部分),

有浮點類型和 小數類型之間不存在隱式轉換。

對於小數,你應該加後綴 'M' 或 'M'

所以,你的情況,通過57.98米而不是57.98

This SO answer列出了各種後綴。

+0

啊。謝謝!我應該停止熬夜編程! – user1721879

相關問題