从List(Of T)中删除对象

时间:2014-12-28 21:49:35

标签: vb.net list visual-studio-2012 methods

我无法从我的列表(联系人)中删除对象

以下是我的字段:

Public Class Contact
'Things to remember 
Private m_firstName As String = String.Empty
Private m_lastName As String = String.Empty 
Private m_address As Address

我的清单:

Public Class ContactManager
Private m_contactRegistry As List(Of Contact)

Public Sub New()
    m_contactRegistry = New List(Of Contact)()
End Sub

我在ContactManger类中的方法。 这里我收到错误“类型'整数'的值无法转换为索引上的Assignment.Contact”

Public Function DeleteContact(index As Integer) As Boolean


    m_contactRegistry.Remove(index)

    Return True
End Function

我的Main类上的删除按钮方法:

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click

        'listResults is my listbox

        Dim list = listResults.SelectedIndex

'm_contact is an object of the Contact class
        m_contacts.DeleteContact(list)
        UpdateGUI()
    End Sub

问题是我不知道如何使用DeleteContact(index As Integer)方法而不会出现错误。你们有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用索引时,您需要RemoveAt()而不是Remove()