473,466 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Removing Items programmatically - using foreach

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

Similar topics

3
by: Walter Zydhek | last post by:
I am having a problem using the NameValueCollection type. If I remove one of the items while iterating through an collection of this type, I end up with an exception. This exception is:...
11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
7
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I'v been struggeling with removing selected items from a listview. Anyone that can give me a piece of code that does this? I am a newbee to this C# and cant figure it out.... Regards...
1
by: Crash | last post by:
Windows XP SP2 C# .NET v1.1 Outlook 2003 {via Office 11.0 PIA} I'm manipulating Outlook's calendar via OLE automation from my C# application. I would like to iterate through the calendar items...
7
by: Keith Patrick | last post by:
After completely giving up on finding some way in my ASP.Net app to take a query string URL and redirect to it as a POST instead, I went with a system like so: Upon "redirection," all the...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
1
by: jobs | last post by:
I have a menu that really does not lend itself to a sitemap datasource or security trimming. In the codebehind i've been able to remove top level menu items with this code: ...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.