|
-
February 21st, 2010, 01:42 PM
#1
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?
-
February 21st, 2010, 02:17 PM
#2
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
-
February 21st, 2010, 06:17 PM
#3
Re: foreach loop problem.
 Originally Posted by memeloo
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.
-
February 21st, 2010, 06:34 PM
#4
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.......
-
February 21st, 2010, 06:36 PM
#5
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.
-
February 21st, 2010, 07:27 PM
#6
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.......
-
February 21st, 2010, 09:28 PM
#7
Re: foreach loop problem.
 Originally Posted by rliq
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.
-
February 21st, 2010, 09:51 PM
#8
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.......
-
February 23rd, 2010, 05:42 PM
#9
Re: foreach loop problem.
 Originally Posted by dovobet
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
-
February 24th, 2010, 05:33 AM
#10
Re: foreach loop problem.
 Originally Posted by JonnyPoet
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!
-
February 24th, 2010, 01:31 PM
#11
Re: foreach loop problem.
 Originally Posted by dovobet
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.
-
February 25th, 2010, 08:53 AM
#12
Re: foreach loop problem.
 Originally Posted by BigEd781
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
-
February 27th, 2010, 10:16 AM
#13
Re: foreach loop problem.
 Originally Posted by JonnyPoet
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
-
February 27th, 2010, 10:51 AM
#14
Re: foreach loop problem.
 Originally Posted by dovobet
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|