Link to home
Start Free TrialLog in
Avatar of Tammu
Tammu

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!!
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

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
Avatar of Tammu
Tammu

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
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
Avatar of Tammu

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
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Tammu

ASKER

Thanks