我正在尝试创建一个发票类型的新对象。我以正确的顺序传入必要的参数。
但是,它告诉我我包含了无效的论点。我可能在这里忽略了一些非常简单的事情,但也许有人可以指出来。
我正在做作业,但是包含 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