Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:04:50+00:00 2026-05-14T15:04:50+00:00

Today I coded a function that uses two nested foreach loops. After seeing, that

  • 0

Today I coded a function that uses two nested foreach loops. After seeing, that it did not work like expected, i debugged it. But I dont see an error, and dont think a simple error can cause the behavior i have noticed.

The part looks like this:

foreach(MyClass cItem in checkedListBoxItemList.Items)
{
   foreach(MyClass cActiveItem in ActiveItemList)
   {
      if (cActiveItem.ID == cItem.ID) /*...check checkbox for item...*/;
   }
}

Lets say, checkedListBoxItemList.items holds 4 items of type MyClass, and ActiveItemList is a List< MyClass > with 2 Items.

The debugger jumps into the outer foreach, reaches inner foreach, executes the if 2 times (once per cActiveItem) and reaches the end of the outer foreach.Now, the debugger jumps back to the head of the outer foreach as it should. But instead of starting the second round of the outer foreach, the debugger suddenly jumps into the MyClass.ToString() method.
I can step through this method 4 times (number of items in checkedListBoxItemList.Items)
and then … nothing. Visual Studio shows me my windows form, and the foreach is not continued.

When changing the code to

int ListCount = checkedListBoxItemList.Items.Count;
for(int i=0; i<ListCount; i++)
{
   MyClass cItem = checkedListBoxItemList.Items[i] as MyClass;
   foreach(MyClass cActiveItem in ActiveItemList)
   {
      if (cActiveItem.ID == cItem.ID) /*...check checkbox for item...*/;
   }
}

everything works fine and as supposed.
I showed the problem to a collegue, but he also didnt understand, what happened. I dont understand why the debugger jumps into the MyClass.ToString() method. I used F10 to step through, so no need to leave the function. And even, if there is a reason, why isnt the foreach loop continued?

Im using Visual Studio 2010, if this is of any matter.

Please tell me what happened. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:04:50+00:00Added an answer on May 14, 2026 at 3:04 pm

    When iterating a collection (using a foreach), the collection you are iterating is not allowed to change; but when you check the checkbox for the matching item, the collection of the outer loop (checkedListBoxItemList.Items) changes and the consequent error that is thrown is probably swallowed somewhere. That more or less explains why you suddenly go into the ToString method and don’t continue the loop.

    When you use a forstatement to iterate, you don’t have that restriction as there is no reference to the collection at the moment you start iterating.

    Hope this explains.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here is a sample Rectangle that I borrowed from MSDN.… May 16, 2026 at 11:17 am
  • Editorial Team
    Editorial Team added an answer Your code doesn't work via normal import either. >>> from… May 16, 2026 at 11:17 am
  • Editorial Team
    Editorial Team added an answer Taken from Mark Murphy's answer on the Android Developers mailing… May 16, 2026 at 11:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm writing an AJAX web app that uses Comet/Long Polling to keep the web
I've seen function calls preceded with an at symbol to switch off warnings. Today
I have never really thought about this until today, but after searching the web
I have a wierd problem that i need to work out how to resolve,
I received a ticket today concerning a broken application that was caused by a
Background: Our web app uses the jquery.constrain.js plugin to handle data entry in some
Later today, I was scrolling through ejhon.com slides and I found out the following:
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
Ok so I'm working with vectors today yaya! well im also working with getcursorpos()
I was browsing a coworkers c# code today and found the following: using (MemoryStream

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.