Avatar of niceoneishere
niceoneishere
 asked on

property or indexer cannot be assigned to it is read only

Hello everyone,

My website is built in C# and ASP.NET 2.0 using sql 2005 database.

I am trying to apply a simple promo code 30% off the subtotal of an order. So I tried using the code like below

protected void btUpdate_Click(object sender, EventArgs e)
    {

        Order currentOrder = Basket.Order;

        string tbcodeValue = this.tbpromo.Text.Trim().ToUpper();

        string[] validCodes = new string[] { "AB30, FF30" };
        if (!((IList<string>)validCodes).Contains(tbcodeValue))
        {
            lbmessage.Text = "Invalid Promo Code. Please enter again";
        }
        else
        {
            currentOrder.PromotionCode = tbcodeValue;
            currentOrder.Subtotal = (0.70 * currentOrder.Subtotal);
            this.lShipping.Text = currentOrder.Shipping.Cost.ToString("c");
            this.lSubtotal.Text = currentOrder.Subtotal.ToString("c");
            this.lTotal.Text = currentOrder.TotalCost.ToString("c");
            lbmessage.Text = "30% discount applied to your order.";
        }
    }

Open in new window


but when I try to test it out, i am getting the following error

 error CS0200: Property or indexer 'Order.Subtotal' cannot be assigned to -- it is read only
   at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)

Open in new window


How can I update the Subtotal.

Thanks a bunch!!
ASP.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
niceoneishere

8/22/2022 - Mon
Ioannis Paraskevopoulos

It seems that the SubTotal is read only. Can you post the definition of the SubTotal property?
Read only properties usually are handled from within the class, so only the class internal operations may modify them. It makes sense that in your case the SubTotal is modified from internal code when you do something like currentOrder.AddItem(ItemNo).

If this is a code you may modify i would suggest creating a method ApplyDiscount(int Percentage) in the currentOrder class and write code in there to handle the discounts.

Giannis
niceoneishere

ASKER
Hi Giannis,

Thanks for replying, in the order class the Subtotal is defined like this

  /// <summary>
    /// gets the cost of the items in the order, exclusive of order-level
    /// charges, such as shipping
    /// </summary>
    public double Subtotal
    {
        get
        {
            if ( _Subtotal < 0 )
            {
                _Subtotal = 0;
                foreach ( Item I in this.Items )
                {
                    _Subtotal += I.Price;
                }
            }
            return _Subtotal;
        }
    }

    /// <summary>
    /// gets the total cost of the order, including all charges, such as
    /// shipping.
    /// </summary>
    public double TotalCost
    {
        get { return this.Subtotal + this.Shipping.Cost; }
    }

Open in new window


so will you let me know where I would create ApplyDiscount method, anyway you can show me an example,

Appreciate it
Ioannis Paraskevopoulos

Hi,

You may have another Property SubtotalDiscount like:

public double SubtotalDiscount{ get; set; }

Open in new window


Your Subtotal property will be:

    public double Subtotal
    {
        get
        {
            if ( _Subtotal < 0 )
            {
                _Subtotal = 0;
                foreach ( Item I in this.Items )
                {
                    _Subtotal += I.Price * (100-SubtotalDiscount) / 100;
                }
            }
            return _Subtotal;
        }
    }

Open in new window


In the constructor of the Order class you should have a default value for SubtoalDiscount setting it to 0:



And the code that will call this will be:
protected void btUpdate_Click(object sender, EventArgs e)
    {

        Order currentOrder = Basket.Order;

        string tbcodeValue = this.tbpromo.Text.Trim().ToUpper();

        string[] validCodes = new string[] { "AB30, FF30" };
        if (!((IList<string>)validCodes).Contains(tbcodeValue))
        {
            lbmessage.Text = "Invalid Promo Code. Please enter again";
        }
        else
        {
            currentOrder.PromotionCode = tbcodeValue;
            currentOrde.SubtotalDiscount = 30;
            this.lShipping.Text = currentOrder.Shipping.Cost.ToString("c");
            this.lSubtotal.Text = currentOrder.Subtotal.ToString("c");
            this.lTotal.Text = currentOrder.TotalCost.ToString("c");
            lbmessage.Text = "30% discount applied to your order.";
        }
    }

Open in new window


Giannis
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
niceoneishere

ASKER
Thanks for replying one question when you say

In the constructor of the Order class you should have a default value for SubtoalDiscount setting it to 0:

you mean something like this correct

	private double _SubtotalDiscount = 0;
	public double SubtotalDiscount
	{
    	get{return _SubtotalDiscount;}
		set
		  {
		    _SubtotalDiscount = value;
		  }
	}

Open in new window


Thanks
ASKER CERTIFIED SOLUTION
Ioannis Paraskevopoulos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ioannis Paraskevopoulos

I think the best would be
Class order
{
Public order
    {
        //do anything before variable assignment

        //assign initial values
        SubtotalDiscount=0;

        //do anything after variable assignment
    }
    public double SubtotalDiscount { get; set; }
}

This way when you call
Order curentOrder = new order;

You make sure that the property will be set to 0.

Giannis
niceoneishere

ASKER
Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.