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

Removing Items programmatically - using foreach

P: n/a
Hi there,
I have a menu (Collection) that needs to be trimmed based on security access
of the
logged user.
protected void AdjustMenuBasedOnUserSecurity(Items ItemsList)
{
foreach (Item i in ItemsList)
{
if (i.Items.Count > 0)
AdjustMenuBasedOnUserSecurity(i.Items);

if (i.Tag == null)
continue;

if (this.LoggedUser.HasPermission(Convert.ToInt32(i.T ag)) == false)
ItemsList.RemoveAt(i.Index);
}
}
=======================
PROBLEM
Altought this proc is recursive, the FOREACH seems to not recalculate the
COUNT of the Items Collection at every run, so when I remove an item from
the list, I also pretty much screws the internal COUNT of the FOREACH.
=======================
SOLUTION
...hummm here is where you may put add some ideas... !!!
Thanks,
Filippo

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Filippo,

You really shouldn't use foreach if you need to modify the contents of
the list (change the state of the enumeration) during the enumeration.
Since you need to remove items, I would recommend using a for loop, counting
backwards from the last item (so that when you remove an item, you don't
have to worry about indexes shifting), like so:

protected void AdjustMenuBasedOnUserSecurity(Items ItemsList)
{
// The item.
Item item;

for (int index = ItemsList.Count - 1; index >= 0; index--)
{
// Get the item.
item = ItemsList[index];

if (i.Items.Count > 0)
AdjustMenuBasedOnUserSecurity(i.Items);

if (i.Tag == null)
continue;

if (this.LoggedUser.HasPermission(Convert.ToInt32(i.T ag)) == false)
ItemsList.RemoveAt(index);
}
}

This should work. This assumes that you implement IList, and have an
indexer.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Filippo P." <pa********@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
Hi there,
I have a menu (Collection) that needs to be trimmed based on security
access of the
logged user.
protected void AdjustMenuBasedOnUserSecurity(Items ItemsList)
{
foreach (Item i in ItemsList)
{
if (i.Items.Count > 0)
AdjustMenuBasedOnUserSecurity(i.Items);

if (i.Tag == null)
continue;

if (this.LoggedUser.HasPermission(Convert.ToInt32(i.T ag)) == false)
ItemsList.RemoveAt(i.Index);
}
}
=======================
PROBLEM
Altought this proc is recursive, the FOREACH seems to not recalculate the
COUNT of the Items Collection at every run, so when I remove an item from
the list, I also pretty much screws the internal COUNT of the FOREACH.
=======================
SOLUTION
..hummm here is where you may put add some ideas... !!!
Thanks,
Filippo

Nov 16 '05 #2

P: n/a
First, I do not think that you can even add or remove collection items in a
foreach loop.

One idea might be to use a simple "for" loop that counts backwards. That
way, when you remove an item, your next position is not affected by the fact
that there is one fewer items.

"Filippo P." <pa********@hotmail.com> wrote in message
news:O#**************@TK2MSFTNGP10.phx.gbl...
Hi there,
I have a menu (Collection) that needs to be trimmed based on security access of the
logged user.
protected void AdjustMenuBasedOnUserSecurity(Items ItemsList)
{
foreach (Item i in ItemsList)
{
if (i.Items.Count > 0)
AdjustMenuBasedOnUserSecurity(i.Items);

if (i.Tag == null)
continue;

if (this.LoggedUser.HasPermission(Convert.ToInt32(i.T ag)) == false) ItemsList.RemoveAt(i.Index);
}
}
=======================
PROBLEM
Altought this proc is recursive, the FOREACH seems to not recalculate the
COUNT of the Items Collection at every run, so when I remove an item from
the list, I also pretty much screws the internal COUNT of the FOREACH.
=======================
SOLUTION
..hummm here is where you may put add some ideas... !!!
Thanks,
Filippo

Nov 16 '05 #3

P: n/a
Is that the description of my ex wife???

jaja.


"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
=======================
SOLUTION
...hummm here is where you may put add some ideas... !!!


As per the C# Programmer's reference:

The foreach statement repeats a group of embedded statements for each
element in an array or an object collection. The foreach statement is used
to
iterate through the collection to get the desired information, but should
not
be used to change the contents of the collection to avoid unpredictable
side
effects.

foreach is like this fabulous babe I work with --> she's great for looking
but you just can't touch...

--Richard

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.