[英]Items not deleting from LIST
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
? 在
MyList.Remove
调用中,您是要删除值2
还是从索引2
删除? If you meant to remove at the index, then use RemoveAt
: 如果要删除索引,请使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.