Solved

Why am I getting an out of rang exception with the following "for loop"?

Posted on 2006-11-02
4
247 Views
Last Modified: 2010-04-16
           int dropDownCount = actionMenu.DropDownItems.Count;
            if(dropDownCount > 6)
            {
                for (int index = 6; index < dropDownCount; index++)
                {
                    actionMenu.DropDownItems.RemoveAt(index);

                }
            }

Thanks.
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17864194
If you are trying to remove all but the first six items, try the following code:

  int dropDownCount = actionMenu.DropDownItems.Count;
  for (int index = 6; index < dropDownCount; index++)
    actionMenu.DropDownItems.RemoveAt(6);

Or,

  int dropDownCount = actionMenu.DropDownItems.Count;
  for (int index = (dropDownCount - 1); index >= 6; index--)
    actionMenu.DropDownItems.RemoveAt(index);
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17864198
Or even,

  while(actionMenu.DropDownItems.Count > 6)
    actionMenu.DropDownItems.RemoveAt(6);
0
 

Author Comment

by:lapucca
ID: 17864199
Nop, I was trying to remove all items after the 6th item.  I got it working now.  You see, the count is changing with each item I remove so it eventually ran out and got the exception error.  Thanks.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 17864204
I believe the reason is that you alter underlying collection, and therefore alter the count of items.

Try to use 'while' loop:

while(actionMenu.DropDownItems.Count>6){
    actionMenu.DropDownItems.RemoveAt(6);
}
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question