473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help deleting a member of an enumeration from within a loop

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
1 1679
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Matthew Wells | last post by:
I'm using this code to delete all relationships in my mdb file iFlag = 1 Do While iFlag <> 0 iFlag = 0 For Each rel In db.Relations db.Relations.Delete rel.Name iFlag = 1 Next rel Loop
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
6
by: Dmitry Karneyev | last post by:
Hi! I try to delete rows from the table in such way foreach(DataRow row in MyDataSet.MyTable) { row.Delete(); } and recive an error that collection is changed and enumeration is impossible....
0
by: Sean Flook | last post by:
I have got some code that can be called from 3 separate places and it works fine for 2 of them, but for the third it gives me the following exception System.InvalidOperationException in...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
2
by: rh0dium | last post by:
Hi all, Why doesn't this work as expected.. I expect that the the lines within the sections will get modified - permanently. It is modifying them during the first section but they never get...
6
by: Yan | last post by:
Here is the code: class A {}; void (A::*A) (); // Line 3 int main() { A a; // Line 6 return 0; }
10
by: braratine | last post by:
Hello, I have to following issue. A type is declared based on an enum: " enum TpAddressPlan { P_ADDRESS_PLAN_NOT_PRESENT = 0, P_ADDRESS_PLAN_UNDEFINED = 1, P_ADDRESS_PLAN_IP = 2,...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.