help deleting a member of an enumeration from within a loop

Discussion in 'ASP .Net' started by Tim, Oct 23, 2003.

  1. Tim

    Tim Guest

    Hi,
    I'm very new to .NET and am programming in C#. I have a web application
    where i have two list boxes. Its kind of like a shopping card where you can
    add items from one 'locations' list box to the 'locations selected' (cart)
    listbox. I have a hirarchy of locations - main locations and sub
    locations. main locations being the parent locations, and the sub locations
    being the child locations.

    I want to be able to check to see if the already selected locations listbox
    (cart) already contains a child location of a parent location that is trying
    to be added to the collection (because there is no need to add the child
    there if the parent is selected). if so the child should be deleted and the
    parent added
    I am looping through the locations selected, and for each one looping
    through the already selected (cart) locations collection and checking the
    existance of the members.
    here is the code:

    private void Button1_Click(object sender, System.EventArgs e)
    {
    if(lstLocations.SelectedIndex != -1)
    {
    foreach(ListItem selectedLocation in lstLocations.Items)
    {
    if(selectedLocation.Selected)
    {
    bool itemAlreadyExists = false;
    bool parentItemExists = false;
    bool childItemExists = false;
    foreach(ListItem locationsSelected in
    lstCart.Items)
    {
    //does new location already exist in
    locations collection?

    if(selectedLocation.Text.Equals(locationsSelected.Text.ToString()))
    {
    itemAlreadyExists = true;
    lblNotice.Text += "At least one of
    the selected items already exists and has been ignored<br>";
    }
    if(!itemAlreadyExists)
    {

    //does parent of child already exist
    in collection

    if(selectedLocation.Value.StartsWith(locationsSelected.Value.ToString()+"-")
    )
    {
    parentItemExists = true;
    lblNotice.Text += "You
    attempted to add a child location of a parent already selected. There is no
    need to add a child location when the parent is already in your collection.
    Item ignored<br>";
    }
    //does child of parent already exist
    in collection, if so get rid of it
    //this is the bit that is giving the
    error!

    if(locationsSelected.Value.StartsWith(selectedLocation.Value.ToString()+"-")
    )
    {
    childItemExists = true;

    lstCart.Items.Remove(locationsSelected); //this is where the error
    occurs

    lstCart.Items.Add(selectedLocation);
    lblNotice.Text += "A child
    location was removed because you added its parent!<br>";
    }
    }

    }
    if(!itemAlreadyExists && !parentItemExists &&
    !childItemExists)
    {
    lstCart.Items.Add(selectedLocation);
    }
    }
    }
    }

    }
    when i execute the page and try add a parent location when a child already
    exists the page throws the following error:

    Collection was modified; enumeration operation may not execute.

    I know this is pretty much saying that as the loop progresses, the code
    tries to delete a member of the collection and hence modify the loop.
    Can you see what i am trying to achieve and please advise me of the best way
    around this problem?

    Thanks alot for your help

    Tim..
     
    Tim, Oct 23, 2003
    #1
    1. Advertising

  2. Hi Tim

    You can try two things:

    Create an array. Add the items you want to remove from the list box to the
    array. Then after the loop remove them.


    Else --- This will be more efficent, but may be error prone; You need to
    think about what will happen if you delete the last item...?

    Use an old style loop, e.g.

    for (int i=0; i<lstLocations.Items.Count; i++)
    {
    ListItem selectedLocation = lstLocations.Items;

    Then when you delete, deceriment i.

    lstCart.Items.Remove(locationsSelected);
    i--;

    Gary

    "Tim" <> wrote in message
    news:%...
    > Hi,
    > I'm very new to .NET and am programming in C#. I have a web application
    > where i have two list boxes. Its kind of like a shopping card where you

    can
    > add items from one 'locations' list box to the 'locations selected' (cart)
    > listbox. I have a hirarchy of locations - main locations and sub
    > locations. main locations being the parent locations, and the sub

    locations
    > being the child locations.
    >
    > I want to be able to check to see if the already selected locations

    listbox
    > (cart) already contains a child location of a parent location that is

    trying
    > to be added to the collection (because there is no need to add the child
    > there if the parent is selected). if so the child should be deleted and

    the
    > parent added
    > I am looping through the locations selected, and for each one looping
    > through the already selected (cart) locations collection and checking the
    > existance of the members.
    > here is the code:
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > if(lstLocations.SelectedIndex != -1)
    > {
    > foreach(ListItem selectedLocation in

    lstLocations.Items)
    > {
    > if(selectedLocation.Selected)
    > {
    > bool itemAlreadyExists = false;
    > bool parentItemExists = false;
    > bool childItemExists = false;
    > foreach(ListItem locationsSelected in
    > lstCart.Items)
    > {
    > //does new location already exist in
    > locations collection?
    >
    > if(selectedLocation.Text.Equals(locationsSelected.Text.ToString()))
    > {
    > itemAlreadyExists = true;
    > lblNotice.Text += "At least one of
    > the selected items already exists and has been ignored<br>";
    > }
    > if(!itemAlreadyExists)
    > {
    >
    > //does parent of child already

    exist
    > in collection
    >
    >

    if(selectedLocation.Value.StartsWith(locationsSelected.Value.ToString()+"-")
    > )
    > {
    > parentItemExists = true;
    > lblNotice.Text += "You
    > attempted to add a child location of a parent already selected. There is

    no
    > need to add a child location when the parent is already in your

    collection.
    > Item ignored<br>";
    > }
    > //does child of parent already

    exist
    > in collection, if so get rid of it
    > //this is the bit that is giving

    the
    > error!
    >
    >

    if(locationsSelected.Value.StartsWith(selectedLocation.Value.ToString()+"-")
    > )
    > {
    > childItemExists = true;
    >
    > lstCart.Items.Remove(locationsSelected); //this is where the error
    > occurs
    >
    > lstCart.Items.Add(selectedLocation);
    > lblNotice.Text += "A child
    > location was removed because you added its parent!<br>";
    > }
    > }
    >
    > }
    > if(!itemAlreadyExists && !parentItemExists

    &&
    > !childItemExists)
    > {
    > lstCart.Items.Add(selectedLocation);
    > }
    > }
    > }
    > }
    >
    > }
    > when i execute the page and try add a parent location when a child already
    > exists the page throws the following error:
    >
    > Collection was modified; enumeration operation may not execute.
    >
    > I know this is pretty much saying that as the loop progresses, the code
    > tries to delete a member of the collection and hence modify the loop.
    > Can you see what i am trying to achieve and please advise me of the best

    way
    > around this problem?
    >
    > Thanks alot for your help
    >
    > Tim..
    >
    >
     
    Gary van der Merwe, Oct 23, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    4
    Views:
    411
    Richard Herring
    Apr 13, 2004
  2. SnuSnu
    Replies:
    6
    Views:
    337
    Steven Rumbalski
    Apr 25, 2004
  3. puvit82
    Replies:
    4
    Views:
    763
    puvit82
    Feb 1, 2008
  4. addi
    Replies:
    0
    Views:
    273
  5. Isaac Won
    Replies:
    9
    Views:
    388
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page