使用关键字“价值”在Set Concector C#中

发布于 2025-01-27 04:10:37 字数 3312 浏览 3 评论 0原文

我正在尝试在设置访问者中使用关键字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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖哒 2025-02-03 04:10:37

这没有道理:

if (value >= 0)
{
    Quantity = value;
}

if (value <= 0)
{
    Quantity = Quantity;
}

为什么要为自己设置财产?那无法实现任何有用的东西。您说,只有当分配的值大于零时,您想设置属性,那么为什么除了大于零以外的任何内容,为什么要检查value

if (value > 0)
{
    Quantity = value;
}

就是这样。

也就是说,如果该值无效,而不是仅仅默默地不设置属性,则您也应该扔grognal ungeofrangeException。这样做的逻辑方式就是这样:

if (value <= 0)
{
    throw new ArgumentOutOfRangeException(...);
}

Quantity = value;

现在,只有在不引发异常时才会设置属性值。

我也只是意识到您没有此属性的支持字段,所以这是错误的。整个事情应该看起来像这样:

private int quantity;

public int Quantity
{
    get { return quantity; }

    set
    {
        if (value <= 0)
        {
            throw new ArgumentOutOfRangeException(...);
        }

        quantity = value;
    }
}

This makes no sense:

if (value >= 0)
{
    Quantity = value;
}

if (value <= 0)
{
    Quantity = Quantity;
}

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?

if (value > 0)
{
    Quantity = value;
}

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:

if (value <= 0)
{
    throw new ArgumentOutOfRangeException(...);
}

Quantity = value;

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:

private int quantity;

public int Quantity
{
    get { return quantity; }

    set
    {
        if (value <= 0)
        {
            throw new ArgumentOutOfRangeException(...);
        }

        quantity = value;
    }
}
猫九 2025-02-03 04:10:37

错误是因为在您的集合{}中,您正在递归调用同一设置器。

        private int quantity; 
        public int Quantity
        {
            get { return  this.quantity; }

            set
            {
                if (value >= 0)
                {
                     this.quantity= value;
                }
            }
        }

        private decimal pricePerItem;
        public decimal PricePerItem
        {
            get
            {
                return  this.pricePerItem;
            }

            set
            {
                if(value >= 0.0m)
                {
                     this.pricePerItem= value;
                }
            }
        }

The error is because in your set {} you are invoking the same setter recursively.

        private int quantity; 
        public int Quantity
        {
            get { return  this.quantity; }

            set
            {
                if (value >= 0)
                {
                     this.quantity= value;
                }
            }
        }

        private decimal pricePerItem;
        public decimal PricePerItem
        {
            get
            {
                return  this.pricePerItem;
            }

            set
            {
                if(value >= 0.0m)
                {
                     this.pricePerItem= value;
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文