我的代码应该从列表中删除项目但不删除项目。
我的代码:
dim myRoom as Room = New Room()
Dim myRoomList as List( of Room ) = new List( of Room )
...
myRoomList.add(myRoom)
msgbox(myRoomList.count)
...
myRoomList.remove(myRoom)
msgbox(myRoomList.count)
答案 0 :(得分:1)
您确定在删除之前没有添加任何项目,因此计数不会再次增加。尝试使用它进行测试...
dim myRoom as Room = New Room()
Dim myRoomList as List( of Room ) = new List( of Room )
...
myRoomList.add(myRoom)
msgbox(myRoomList.count)
...
msgbox(myRoomList.count)
myRoomList.remove(myRoom)
msgbox(myRoomList.count)
答案 1 :(得分:1)
唯一可能的原因是,如果您错误地覆盖Equals
类的Room
方法,则不会删除该项。例如,以下代码将表现出这样的行为:
Class Room
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return False
End Function
End Class
如果情况并非如此,则错误发生在其他地方,而且您向我们展示的代码确实有效。