0

I recently needed to modify someone's code that used multiple continue cases in a for each. The addition was a new control loop inside the for each, which promptly broke the continue logic. Is there a good way to get the next list item in a such a loop without rewriting all of the continue cases?

// Additional control loops within the member function which cannot be 
// turned into functions due to native C++ data types.
{
    for each(KeyValuePair<String^,String^> kvp in ListOfItems) {
        do { // new condition testing code
           // a bunch of code that includes several chances to continue
        } while (!reachedCondition)
    }
}
Jonas
  • 121,568
  • 97
  • 310
  • 388
nimchimpsky
  • 99
  • 11
  • Possible duplicate of [Can I use break to exit multiple nested for loops?](https://stackoverflow.com/questions/1257744/can-i-use-break-to-exit-multiple-nested-for-loops) – Tripp Kinetics Apr 04 '18 at 19:37
  • If C++/CLI *does* support named loops, that renders the question trivial. Just name the loops. – Tripp Kinetics Apr 04 '18 at 19:58

1 Answers1

1

continue and break go to the inner most control loop. I've used the iterator within a for loop. So depending upon what your ListOfItems is (i.e. SortedList or Dictionary ...) you might be able to iterate instead of continue.

int i=0;
Dictionary^ d = gcnew Dictionary<string, string>();

for (IEnumerator<KeyValuePair<string, string>>^ e =  d->GetEnumerator();i< d->Count;i++) {
    // do stuff
    e->MoveNext();
}
codebender
  • 469
  • 1
  • 6
  • 23