CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2009
    Posts
    90

    foreach loop problem.

    Im havign an odd problem where a foreach loop that is supposed to delete all records only deletes half of the records.

    foreach (Microsoft.Office.Interop.Outlook._ContactItem item in folder.Items)
    {
    item.Delete();
    MessageBox.Show("contact gone");
    }

    the folder.Items is a MAPfolder ive created in outlook that has contacts inside it.

    The loop will only delete half of the contacts from the folder. If I run the method twice, it then removes allof them. Why is this happening?

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: foreach loop problem.

    any exceptions are thrown?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Dec 2009
    Posts
    90

    Re: foreach loop problem.

    Quote Originally Posted by memeloo View Post
    any exceptions are thrown?
    Nope nothing, all it does is do half of the collection. Then next time the method is ran. It will do half again.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: foreach loop problem.

    You are not allowed to modify the variable being used as the iterator in foreach loop. In your case the iterator is 'item' and you are deleting it in the loop.

    Try:
    Code:
    for (Int32 i = 0; i < folder.Items.Count; i++)
        folder.Items[i].Delete();
    I guessed at 'Count', it may be 'Length' or some other property that gives the size of the collection.

    There may even be a Clear() method, which may delete all of the items in the collection in one go.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: foreach loop problem.

    You can't delete items while iterating through a collection.

    Generally you solve this problem by identifying the items you wish to delete, copy them to a temp collection, iterate through the temp collection and delete each item in the original collection.

  6. #6
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: foreach loop problem.

    If you are able to use LINQ on the collection, I believe you can delete them too.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  7. #7
    Join Date
    Dec 2009
    Posts
    90

    Re: foreach loop problem.

    Quote Originally Posted by rliq View Post
    You are not allowed to modify the variable being used as the iterator in foreach loop. In your case the iterator is 'item' and you are deleting it in the loop.

    Try:
    Code:
    for (Int32 i = 0; i < folder.Items.Count; i++)
        folder.Items[i].Delete();
    I guessed at 'Count', it may be 'Length' or some other property that gives the size of the collection.

    There may even be a Clear() method, which may delete all of the items in the collection in one go.
    I had tried that however there is no Delete() method that can be invoked on the item in the collection. Neither is there a clear() method. There is a remove method but this does not seem to do anything either. I used the line below

    cfolder.Items.Remove(i);

    Arjay - Im not sure i understand how that will work. Wont it mean i am iterating through the collection i will delete anyway?
    Last edited by dovobet; February 21st, 2010 at 09:36 PM.

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: foreach loop problem.

    dovobet,

    In your example, you were calling the Delete() member, so I assumed that there was one?

    Maybe you need:
    Code:
    for (Int32 i = 0; i < folder.Items.Count; i++)
    {
        Microsoft.Office.Interop.Outlook._ContactItem contactItem = folder.Items[i] as Microsoft.Office.Interop.Outlook._ContactItem contactItem;
        contactItem.Delete();
    }
    I assume there is no "folder.Items.Clear()" member?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: foreach loop problem.

    Quote Originally Posted by dovobet View Post
    I had tried that however there is no Delete() method that can be invoked on the item in the collection. Neither is there a clear() method. There is a remove method but this does not seem to do anything either. I used the line below

    cfolder.Items.Remove(i);

    Arjay - Im not sure i understand how that will work. Wont it mean i am iterating through the collection i will delete anyway?
    During trying to delete items I would always suggest to go through the list from the end to the beginning. This makes sure the index of the items to delete will not be changed while deleting the items.
    The remove method should work then.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: foreach loop problem.

    Quote Originally Posted by JonnyPoet View Post
    During trying to delete items I would always suggest to go through the list from the end to the beginning. This makes sure the index of the items to delete will not be changed while deleting the items.
    The remove method should work then.
    I was just about to say that
    It's not a bug, it's a feature!

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: foreach loop problem.

    Quote Originally Posted by dovobet View Post
    Arjay - Im not sure i understand how that will work. Wont it mean i am iterating through the collection i will delete anyway?
    But you are not using an iterator as you are with a foreach loop. Also, as long as the Delete() method does not modify the collection (but rather, the object itself) your foreach loop would be fine.

  12. #12
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: foreach loop problem.

    Quote Originally Posted by BigEd781 View Post
    But you are not using an iterator as you are with a foreach loop. Also, as long as the Delete() method does not modify the collection (but rather, the object itself) your foreach loop would be fine.
    But he simple wants to modify the collection so he cannot iterate but should do
    Code:
    for (Int32 i = folder.Items.Count-1;i>= 0; i--)
    {
    
    cfolder.Items.Remove(i);
    
    }
    Simple moving from the last to the first item and deleting (= removing from collection) them, Thats all.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  13. #13
    Join Date
    Dec 2009
    Posts
    90

    Re: foreach loop problem.

    Quote Originally Posted by JonnyPoet View Post
    But he simple wants to modify the collection so he cannot iterate but should do
    Code:
    for (Int32 i = folder.Items.Count-1;i>= 0; i--)
    {
    
    cfolder.Items.Remove(i);
    
    }
    Simple moving from the last to the first item and deleting (= removing from collection) them, Thats all.
    That was the problem. I had to start frm the end and work upwards through the collection. Weird c#. Thank you for the help guys

  14. #14
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: foreach loop problem.

    Quote Originally Posted by dovobet View Post
    That was the problem. I had to start frm the end and work upwards through the collection. Weird c#. Thank you for the help guys
    This is not only a C# problem. Its more a problem of collections and their indexes.
    If you have an indexed collection you normally dont want to have a missing index is the list isn't it? So if someone removes an Item all the items after that one are getting a new index. And thats the reason why you cannot
    a) remove items by use of an iterator
    b) If you want to remove items you have to go from the end to the first one, standardway- always.
    There is only one exception: If you want to remove all of them remove always the first one and count the remaining items in the collection. Do that as long as the items Count are more then zero
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured