C# crew i need some help

Discussion in 'OT Technology' started by TUNNGE, Mar 28, 2008.

  1. TUNNGE

    TUNNGE New Member

    Joined:
    Nov 21, 2003
    Messages:
    3,990
    Likes Received:
    0
    Location:
    Georgia
    So I'm working on a project for school where we're making some kind of shopping cart program. The only parts I'm having trouble with are the add button and remove button. When you click the add button it's supposed to move the selected item from the inventory into your shopping cart. It does that. But then when I try to add the same item, instead of it updating the current item in the cart with a new quantity, it adds a new line with the new quantity. Looks like:

    1 apple
    2 apple

    when I try and add an apple twice.

    Then my remove button just doesn't work. It decreases the quantity but it won't remove the item when the quantity is at 0.

    Help please? Here's my code

    Add Button

    Code:
    
    private void addButton_Click(object sender, EventArgs e)
            {
                GroceryItem current = (GroceryItem)InventoryBox.SelectedItem;
                current.IncreaseQuantity();
                CartBox.Items.Add(current);
            }
    
    I know I have to have some kind of loop to go through the list of items in the shopping cart but I can't figure out a loop that has worked yet.

    Remove Button

    Code:
     private void removeButton_Click(object sender, EventArgs e)
            {
                GroceryItem current = (GroceryItem)CartBox.SelectedItem;
                current.DecreaseQuantity();
                CartBox.Items.Add(current);
                if (current.GetQuantity() == 0)
                {
                    CartBox.Items.Remove(current);
                }
            }
    
    I figured that if statement would remove any item whose quantity was 0 or below...but it doesn't. It even goes into the negatives if I keep clicking the remove button

    So yeah any help would be great...:x:
     
  2. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Post all the code for your form.
     
  3. n1nj3w

    n1nj3w New Member

    Joined:
    Jul 2, 2007
    Messages:
    24
    Likes Received:
    0
    On the add method remove the old current before you add the new one.

    Again in the remove code you add current before you remove anything. So when you do remove it, only the first instance is removed.

    With out more code I can't be sure on this but from small amount of code I am looking at these would be my guesses.
     

Share This Page