Home > Enterprise >  Using keyword "value" in set accessor C#
Using keyword "value" in set accessor C#

Time:05-06

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($"\nPart Number: {PartNumber}");

            Console.WriteLine($"Unit Price: {PricePerItem:C}");

            Console.WriteLine($"Quantity: {Quantity}");

            Console.WriteLine($"Part Description: {PartDescription}");

            Console.WriteLine($"Total price: {total:C}");
        }

 
    }
}

CodePudding user response:

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;
    }
}

CodePudding user response:

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;
                }
            }
        }
  • Related