I have a list that I am using to hold a queue of commands.
I need to loop though each item of the queue, executing the commands, deleting them as it goes along.
The items weren't being deleted from the queue.
Here is a quick test code which doesn't work either:
Dim MyList As New List(Of String)
For i = 0 To 5
MyList.Add(i)
Next
MsgBox(MyList.Count) 'returns 6
MyList.Remove(2)
MyList.Remove(4)
For i = 0 To MyList.Count - 1
MsgBox(MyList(i)) 'pops up 6 times, including the 2 "deleted" ones.
Next
MsgBox(MyList.Count) 'still returns 6
The items don't get deleted.
In your MyList.Remove
calls, do you mean to remove the value 2
or remove at the index of 2
? If you meant to remove at the index, then use RemoveAt
:
MyList.RemoveAt(2)
Otherwise, if your code is actually exactly as you've posted, then it should be working fine. I plugged this into a little console app, and it worked:
Sub Main()
Dim MyList As New List(Of String)
For i = 0 To 5
MyList.Add(i)
Console.WriteLine("i: {0}", i)
Next
Console.WriteLine("MyList.Count: {0}", MyList.Count)
MyList.Remove(2)
MyList.Remove(4)
For i = 0 To MyList.Count - 1
Console.WriteLine("MyList({0}): {1}", i, MyList(i))
Next
Console.WriteLine("MyList.Count: {0}", MyList.Count)
Console.Read()
End Sub
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
MyList.Count: 6
MyList(0): 0
MyList(1): 1
MyList(2): 3
MyList(3): 5
MyList.Count: 4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.