help deleting a member of an enumeration from within a loop

T

Tim

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..
 
G

Gary van der Merwe

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top