-1

I want to make a simple online store that can buy multiple items. Here is my code

public void BuyItem(int deviceid, int quantity)
    {
        Dictionary<int, int> devicelist = new Dictionary<int, int>();
        devicelist.Add(deviceid, quantity);

        Device devices = (from device in master.Devices
                      where device.IDDevice == deviceid
                      select device).SingleOrDefault();
        customer = os.GetCustomer(User);
        //List<CartShop> cartList = new List<CartShop>();
        //var toCart = devices.ToList();
        //foreach (var dataCart in toCart)
        //{
            cartList.Add(new CartShop
            {
                IDDevice = deviceid,
                IDLocation = devices.IDLocation,
                IDCustomer = customer,
                Name = devices.Name,
                Quantity = quantity,
                Price = Convert.ToInt32(devices.Price) * quantity
            });
            cartTotal = cartList;
            StoreTransaksi.DataSource = new BindingList<CartShop>(cartTotal);
            StoreTransaksi.DataBind();
        //}
        X.Msg.Show(new MessageBoxConfig
        {
            Buttons = MessageBox.Button.OK,
            Icon = MessageBox.Icon.INFO,
            Title = "INFO",
            Message = "Success"
        });
    }

But it only can add 1 item, after choose the other item, it replace the old one. (Could not add more than one). Please help

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Allegra
  • 95
  • 1
  • 3
  • 9
  • this may be happening since every time BuyItem method is invoked carList may be getting Initialised everytime, – abhi Dec 28 '16 at 08:09
  • That's why I make another List `cartTotal = cartList`; that store what is in cartList. But it still doesn't work – Allegra Dec 28 '16 at 08:11
  • 1
    I'm guessing that cartList is initialised in PageInit or PageLoad event, so it's new every time. `cartTotal = cartList` just copies one list into another. You should keep carList in Session... – Nino Dec 28 '16 at 08:22

1 Answers1

1

The issue is that cartTotal is the same as cartList (take a look at this). You need to do the following for copying a list to another without keeping the reference:

cartTotal = new list<cartShop>(cartList);

Also note that this is still in the method and will be created each time you call the method.

Update: This is a very simple console application that does what you need:

internal class Program
{
    public static List<Item> ShoppingCart { get; set; }

    public static void Main()
    {
        ShoppingCart = new List<Item>();
        AddToCart(new Item() { ProductId = 2322, Quantity = 1 });
        AddToCart(new Item() { ProductId = 5423, Quantity = 2 });
        AddToCart(new Item() { ProductId = 1538, Quantity = 1 });
        AddToCart(new Item() { ProductId = 8522, Quantity = 1 });
    }

    public static void AddToCart(Item item)
    {
        ShoppingCart.Add(new Item() { ProductId = item.ProductId, Quantity = item.Quantity});
    }
}

public class Item
{
    public int ProductId { get; set; }
    public int Quantity { get; set; }
}
Community
  • 1
  • 1
Mahdi
  • 3,199
  • 2
  • 25
  • 35
  • It still not working, the carTotal still begin from 0 and not store the old value. cartList and carTotal are being global variable – Allegra Dec 28 '16 at 09:09
  • If cartList is global, what is cartTotal for? Just remove `cartTotal = cartList;` – Mahdi Dec 28 '16 at 09:26
  • cartTotal is for copying a list from cartList since it always new every time. I've been removed `cartTotal = cartList;` – Allegra Dec 28 '16 at 09:38