使用关键字“价值”在Set Concector C#中
我正在尝试在设置访问者中使用关键字value
,只要用户输入的值大于0,我就想将其设置为变量ventity
。
我似乎找不到我做错了什么。对于此Quantity = value;
,我一直在收到回溯错误。希望有人能看到我没有的东西。谢谢。
using System;
namespace Invoice
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("How many parts would you like to " +
"enter into the system: ");
int newParts = int.Parse(Console.ReadLine());
Invoice[] invoice = new Invoice[newParts];
for (int i = 0; i < newParts; i++)
{
invoice[i] = new Invoice();
Console.WriteLine("Enter the part number: ");
invoice[i].PartNumber = Console.ReadLine();
Console.WriteLine("Enter description of item: ");
invoice[i].PartDescription = Console.ReadLine();
Console.WriteLine("Enter the quantity: ");
invoice[i].Quantity = int.Parse(Console.ReadLine());
Console.WriteLine("Enter in the price of the item: ");
invoice[i].PricePerItem = decimal.Parse(Console.ReadLine());
}
for (int i = 0; i < newParts; i++)
{
invoice[i].DisplayOrder();
}
}
}
}
using System;
namespace Invoice
{
public class Invoice
{
public string PartNumber { get; set; }
public string PartDescription { get; set; }
public int Quantity
{
get { return Quantity; }
set
{
if (value >= 0)
{
Quantity = value;
}
if (value <= 0)
{
Quantity = Quantity;
}
}
}
public decimal PricePerItem
{
get
{
return PricePerItem;
}
set
{
if(value >= 0.0m)
{
PricePerItem = value;
}
if (value <= 0.0m)
{
PricePerItem = PricePerItem;
}
}
}
public Invoice(String PartNumber, String PartDescription, int Quantity, decimal PricePerItem)
{
this.PartNumber = PartNumber;
this.PartDescription = PartDescription;
this.Quantity = Quantity;
this.PricePerItem = PricePerItem;
}
public Invoice()
{
}
public decimal GetInvoiceAmount(int numberOfItems, decimal priceOfItem)
{
return numberOfItems * priceOfItem;
}
public void DisplayOrder()
{
decimal total = GetInvoiceAmount(Quantity, PricePerItem);
// Display Receipt
Console.Write("\nOrder Receipt: ");
Console.WriteLine($"\nPart Number: {PartNumber}");
Console.WriteLine($"Unit Price: {PricePerItem:C}");
Console.WriteLine($"Quantity: {Quantity}");
Console.WriteLine($"Part Description: {PartDescription}");
Console.WriteLine($"Total price: {total:C}");
}
}
}
I'm trying to use the keyword value
in the set accessor and as long as the user entered value is greater than 0, I want to set it to the variable Quantity
.
I can not seem to find what it is I am doing wrong. I keep getting a traceback error to for this Quantity = value;
. Hoping someone can see what I don't. Thanks.
using System;
namespace Invoice
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("How many parts would you like to " +
"enter into the system: ");
int newParts = int.Parse(Console.ReadLine());
Invoice[] invoice = new Invoice[newParts];
for (int i = 0; i < newParts; i++)
{
invoice[i] = new Invoice();
Console.WriteLine("Enter the part number: ");
invoice[i].PartNumber = Console.ReadLine();
Console.WriteLine("Enter description of item: ");
invoice[i].PartDescription = Console.ReadLine();
Console.WriteLine("Enter the quantity: ");
invoice[i].Quantity = int.Parse(Console.ReadLine());
Console.WriteLine("Enter in the price of the item: ");
invoice[i].PricePerItem = decimal.Parse(Console.ReadLine());
}
for (int i = 0; i < newParts; i++)
{
invoice[i].DisplayOrder();
}
}
}
}
using System;
namespace Invoice
{
public class Invoice
{
public string PartNumber { get; set; }
public string PartDescription { get; set; }
public int Quantity
{
get { return Quantity; }
set
{
if (value >= 0)
{
Quantity = value;
}
if (value <= 0)
{
Quantity = Quantity;
}
}
}
public decimal PricePerItem
{
get
{
return PricePerItem;
}
set
{
if(value >= 0.0m)
{
PricePerItem = value;
}
if (value <= 0.0m)
{
PricePerItem = PricePerItem;
}
}
}
public Invoice(String PartNumber, String PartDescription, int Quantity, decimal PricePerItem)
{
this.PartNumber = PartNumber;
this.PartDescription = PartDescription;
this.Quantity = Quantity;
this.PricePerItem = PricePerItem;
}
public Invoice()
{
}
public decimal GetInvoiceAmount(int numberOfItems, decimal priceOfItem)
{
return numberOfItems * priceOfItem;
}
public void DisplayOrder()
{
decimal total = GetInvoiceAmount(Quantity, PricePerItem);
// Display Receipt
Console.Write("\nOrder Receipt: ");
Console.WriteLine(quot;\nPart Number: {PartNumber}");
Console.WriteLine(quot;Unit Price: {PricePerItem:C}");
Console.WriteLine(quot;Quantity: {Quantity}");
Console.WriteLine(quot;Part Description: {PartDescription}");
Console.WriteLine(quot;Total price: {total:C}");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这没有道理:
为什么要为自己设置财产?那无法实现任何有用的东西。您说,只有当分配的值大于零时,您想设置属性,那么为什么除了大于零以外的任何内容,为什么要检查
value
?就是这样。
也就是说,如果该值无效,而不是仅仅默默地不设置属性,则您也应该扔
grognal ungeofrangeException
。这样做的逻辑方式就是这样:现在,只有在不引发异常时才会设置属性值。
我也只是意识到您没有此属性的支持字段,所以这是错误的。整个事情应该看起来像这样:
This makes no sense:
Why would you set a property to itself? That can't achieve anything useful. You say that you want to set the property if and only if the assigned value is greater than zero, so why would you be checking
value
for anything but being greater than zero?That's it, that's all.
That said, you also ought to be throwing an
ArgumentOutOfRangeException
if the value is not valid, rather than just silently not setting the property. The logical way to do that would be like so:Now the property value will only be set if an exception is not thrown.
I also just realised that you have no backing field for this property, so that's wrong. The whole thing should look like this:
错误是因为在您的集合{}中,您正在递归调用同一设置器。
The error is because in your set {} you are invoking the same setter recursively.