Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing an object from a list

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello,

VS 2005

I have the following code below and I would like to remove an object from the list.

Code:
For Each timerToStop As Windows.Forms.Timer In emailTimerArray
                If (timerToStop.Tag = callID) Then 'Stop this timer
                    timerToStop.Stop()
                    timerToStop.Enabled = False
                    emailTimerArray.Remove(timerToStop) 'Error
                End If
            Next

The error message i am getting is: "Collection was modified; enumeration operation may not execute"
Code:
Dim emailTimerArray As New List(Of Windows.Forms.Timer)


The way I am filling
Code:
emailTimer = New System.Windows.Forms.Timer
            emailTimer.Interval = 25000
            emailTimer.Enabled = True
            emailTimer.Tag = e.CallID
            emailTimerArray.Add(emailTimer)

Many thanks for any help,

Steve
 
I believe the error you are getting is actually occuring on the Next line. This is because it is finding that the List has changed in that cycle of the for loop.

It seems that you only want to delete one item out of the List. Try this instead:

Code:
For Each timerToStop As Windows.Forms.Timer In emailTimerArray
                If (timerToStop.Tag = callID) Then 'Stop this timer
                    timerToStop.Stop()
                    timerToStop.Enabled = False
                    emailTimerArray.Remove(timerToStop)
                    Exit For
                End If
            Next
 
Alternatively, instead of using For Each, iterate backwards through the array i.e

For x = NumberOfElementsInList-1 To 0 Step -1

(not forgetting Exit For as Aptitude pointed out)


Hope this helps.

[vampire][bat]
 
When going thru a collection backwards you do not need "exit for" if you find a match. This is how you remove multiple items.
 
waynespangler, if you only want to remove one item, there is no point in iterating through the entire list - once the item has been removed, the job is done so Exit For.

steve1rm, working backwards through the list maintains the integrity of the list, but does add its own problems, each element will need to be cast to the appropriate type before it can be used:

[tt]
'Not tested but should do what you need
Dim timerToStop As Windows.Forms.Timer
For t As Integer = emailTimerArray.Length - 1 To 0 Step - 1
timerToStop = CType(emailTimerArray(t), Windows.Forms.Timer)
If timerToStop.Tag = callID Then 'Stop this timer
timerToStop.Stop()
timerToStop.Enabled = False
emailTimerArray.RemoveAt(t)
Exit For
End If
Next
[/tt]


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top