John M
John M

Reputation: 3

Removing an object from List (Of T)

I'm not able to remove an object from my List (of contact)

Here are my fields:

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

My list:

Public Class ContactManager
Private m_contactRegistry As List(Of Contact)

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

My method in ContactManger Class. Here I'm getting error "Value of type 'Integer' cannot be converted to Assignment.Contact" on the index

Public Function DeleteContact(index As Integer) As Boolean


    m_contactRegistry.Remove(index)

    Return True
End Function

My delete button method on my Main class:

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

The problem is that I don't know how to do the method DeleteContact(index As Integer) without getting an error. Do you guys have a suggestion?

Upvotes: 0

Views: 98

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

When using an index, you need RemoveAt() rather than Remove()

Upvotes: 1

Related Questions