473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing Items programmaticall y - using foreach

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

if (i.Tag == null)
continue;

if (this.LoggedUse r.HasPermission (Convert.ToInt3 2(i.Tag)) == false)
ItemsList.Remov eAt(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 28690
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 AdjustMenuBased OnUserSecurity( 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)
AdjustMenuBased OnUserSecurity( i.Items);

if (i.Tag == null)
continue;

if (this.LoggedUse r.HasPermission (Convert.ToInt3 2(i.Tag)) == false)
ItemsList.Remov eAt(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.co m

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

if (i.Tag == null)
continue;

if (this.LoggedUse r.HasPermission (Convert.ToInt3 2(i.Tag)) == false)
ItemsList.Remov eAt(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********@hot mail.com> wrote in message
news:O#******** ******@TK2MSFTN GP10.phx.gbl...
Hi there,
I have a menu (Collection) that needs to be trimmed based on security access of the
logged user.
protected void AdjustMenuBased OnUserSecurity( Items ItemsList)
{
foreach (Item i in ItemsList)
{
if (i.Items.Count > 0)
AdjustMenuBased OnUserSecurity( i.Items);

if (i.Tag == null)
continue;

if (this.LoggedUse r.HasPermission (Convert.ToInt3 2(i.Tag)) == false) ItemsList.Remov eAt(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*****@discus sions.microsoft .com> wrote in message
news:AF******** *************** ***********@mic rosoft.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
9337
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: Collection was modified after the enumerator was instantiated. This happens when it attempts to continue through the enumeration.
11
2148
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 datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to...
6
16970
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 through a dictionary, looking for things, then remove them.... John
3
3107
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 list box. The problem is that if there are many items selected (thousands), then removing the items from the right side list box takes for ever...
7
34767
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 Martin Arvidsson
1
4270
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 collection and delete appointments from the items collection based on some business rules. However after I delete/remove items I am getting...
7
2713
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 QueryString pairs are placed in HttpContext.Current.Items and retrieved with a wrapper for Request.Params that includes checks there. This works in general...
6
6091
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 more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
1
6621
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: Menu.Items.Remove(Menu.FindItem("Admin")) But how can I remove child menu items under the Admin entry in the same way?
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
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 we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.