Solved

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

Posted on 2006-11-02
4
242 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
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
Comment Utility
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
Comment Utility
Or even,

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

Author Comment

by:lapucca
Comment Utility
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 29

Expert Comment

by:anarki_jimbel
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now