我是C#的新手,对我来说真的很难。我正在用C#编写这个程序,这是我面临的错误。
错误1'Lab1.Invoice'不包含'partNumber'的定义,并且没有扩展方法'partNumber'可以找到类型'Lab1.Invoice'的第一个参数(你是否缺少using指令或程序集)参考?)
这是我的代码:
using System;
namespace Lab1
{
class InvoiceTest
{
static void Main(string[] args)
{
string partNumber, partDescription;
int quantity;
decimal Iprice;
Console.WriteLine("Part Number:");
partNumber = Console.ReadLine();
Console.WriteLine("Part Description:");
partDescription = Console.ReadLine();
Console.WriteLine("Quantity:");
quantity = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Price:");
Iprice = Convert.ToDecimal(Console.ReadLine());
Invoice invoice = new Invoice(partNumber, partDescription, quantity, Iprice);
Console.WriteLine("Order: ");
Console.WriteLine("Part Number: {0}", invoice.partNumber);
Console.WriteLine("Description: {0}", invoice.pDescription);
Console.WriteLine("Quantity: {0}", invoice.Quantity);
Console.WriteLine("Price: {0}", invoice.Price);
Console.WriteLine("Total: {0}", invoice.GetInvoiceAmount());
}
}
}
Here is the code for Invoice:<br>
namespace Lab1
{
class Invoice
{
private int qty;
private decimal price;
private string pNumber { get; set; }
private string pDescription { get; set; }
public Invoice(string partNumber, string partDescription, int quantity, decimal Iprice)
{
pNumber = partNumber;
pDescription = partDescription;
Quantity = quantity;
Price = Iprice;
}
public int Quantity
{
get
{
return qty;
}
set
{
if (value > 0)
qty = value;
else
qty = 1;
}
}
public decimal Price
{
get
{
return price;
}
set
{
if (value > 0)
price = value;
else
price = 1;
}
}
public decimal GetInvoiceAmount()
{
return Quantity * Price;
}
}
}
答案 0 :(得分:0)
在let sortDescriptor = NSSortDescriptor(key: "createDate", ascending: false)
课程中添加名为PartNumber
的公共媒体资源和名为PartDescription
的公共媒体资源。
答案 1 :(得分:0)
正如错误所说:
Invoice
不包含名为partNumber
的属性。
您通过构造函数发送它,但将其分配给名为pNumber
使用它或将其重命名为PartNumber
编辑:
刚注意到它是私有财产 - 也将其更改为public
(您可以离开设置者private
)
答案 2 :(得分:0)
只是添加你已经获得的评论,如果你想拥有一个私有的setter,那么用私有的setter创建一个属性。
class Invoice
{
private int qty;
private decimal price;
public string partNumber { get; private set; }
...
}
答案 3 :(得分:0)
我改变了:
private string pNumber to a public string pNumber
以便访问它。我为pDescription