By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

help deleting a member of an enumeration from within a loop

P: n/a
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(locationsSele cted.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(selectedLoca tion.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..
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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[i];

Then when you delete, deceriment i.

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

Gary

"Tim" <ti***@broadbanned.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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(locationsSele cted.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(selectedLoca tion.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..

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.