Question Remove an item from a Collection

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
Hello.

I need your help guys. i need to remove an item from a collection. And i know that
I can't delete an item or an object from a collection. But i can add the object that i want to delete/remove to another collection.
But i'm a bit week in coding. so can anyone help me please?

Thank you :)

I appreciate any help

this is my code that i wrote so far:

Public Class Form1
    Dim _collection As Collection
    Dim _icollection As IList(Of _schoolinfo)
    Private index As Int16 = 0
    Dim _inf As New _schoolinfo
    Dim _infoschool As String
    Dim _info As _schoolinfo


 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        _icollection = New List(Of _schoolinfo)
        _collection = New Collection
        RefreshControls("N")
        PrepareTextBox(False)
        Me.txtWelcome.Text = pbuserName
        ' _loginForm.Hide()
        Me.btnNew.Focus()
    End Sub

 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        _info = New _schoolinfo
        With _info
            .Code = txtCode.Text
            .Name = txtName.Text
            .PhoneNumber = txtPhoneNumber.Text
            .Address = txtAddress.Text
            .Title = cmbTitle.Text
            .Position = cmbPosition.Text
            .DateTime = txtDate.Text
            If RDFemale.Checked Then
                .Sex = "Female"
                If RDMale.Checked Then
                    .Sex = "Male"
                End If
            End If
        End With
        If _infoschool = "I" Then
            index = _collection.Count + 1
            _icollection.Add(New _schoolinfo With {.Code = txtCode.Text, .Name = txtName.Text, .PhoneNumber = txtPhoneNumber.Text, .Address = txtAddress.Text, .Title = cmbTitle.Text, .Position = cmbPosition.Text, .DateTime = txtDate.Text})
            _collection.Add(_info, getkey(index))
        Else
            '_info = New _schoolinfo
            If _collection Is Nothing Then _collection = New Collection
            If _collection.Count > 0 Then _collection.Remove(getkey(index))
            _collection.Add(_info, getkey(index))
        End If
        _inf = Nothing
        PrepareTextBox(False)
        RefreshControls("L")
    End Sub



  Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        '----------------------------------------------------------------
    End Sub
 
Last edited by a moderator:
i know that I can't delete an item or an object from a collection.
Um, you can't know that because it's not true. If you want remove an item from a collection then you call the Remove method of the collection. Done!

Also, don't EVER use that Collection class. We're not using VB6 here. If you need a collection that acts as a dynamic array then you use a List(Of T), where T is the type of the items you want to store. You already have such a collection so get rid of the other one altogether.
 
I hate dealing with collections.
But it's an assignment.
i need to remove a record from the collection.
i managed to get here :(And i am stuck :(



Dim _inf As New _schoolinfo
With _inf
'If .Code = txtCode.Text And .Name = txtName.Text And .PhoneNumber = txtPhoneNumber.Text And .Position = cmbPosition.Text And (.Sex = RDFemale.Checked Or RDMale.Checked) And .Title = cmbTitle.Text And .DateTime = txtDate.Text Then
_collection.Add(_info, getkey(index))
_collection.Remove(getkey(index))
For i As Integer = getkey(index) To i = _collection.Count
index = index - 1
Next
'End If
End With
_icollection.Remove(New _schoolinfo With {.Code = txtCode.Text, .Name = txtName.Text, .PhoneNumber = txtPhoneNumber.Text, .Address = txtAddress.Text, .Title = cmbTitle.Text, .Position = cmbPosition.Text, .DateTime = txtDate.Text})


Private Function getkey(ByVal ind As Int16) As String
Return "K_" & ind.ToString
End Function



Thank you for your time :(

And sorry about the mess. by mistake
 
Last edited:
Back
Top