Collection Class Remove method 
Author Message
 Collection Class Remove method

Hi. I am having a problem removing members from a collection class.
Here's my code for the collection class, and the object being
collected:

Public Class TestCol
    Inherits CollectionBase

    Default Public Overloads ReadOnly Property Item(ByVal index As
Integer) As TestOBJ
        Get
            Return CType(MyBase.List(index), TestOBJ)
        End Get
    End Property

    Public Sub Add(ByVal Name As String)
        Dim oTestOBJ As New TestOBJ()
        oTestOBJ.Name = Name
        MyBase.List.Add(oTestOBJ)

    End Sub

    Public Sub Delete(ByVal Name As String)
        Dim oTestOBJ As New TestOBJ()
        oTestOBJ.Name = Name
        MyBase.List.Remove(oTestOBJ)

    End Sub

End Class

Public Class TestOBJ
    Private m_Name As String

    Public Shadows Function equals(ByVal obj As Object) As Boolean
        Dim oTestOBJ As TestOBJ
        oTestOBJ = CType(obj, TestOBJ)
        Return oTestOBJ.Name = m_Name

    End Function

    Public Shared Shadows Function equals(ByVal obj1 As Object, ByVal
obj2 As Object) As Boolean
        Dim oTestOBJ1, oTestOBJ2 As TestOBJ
        oTestOBJ1 = CType(obj1, TestOBJ)
        oTestOBJ2 = CType(obj2, TestOBJ)
        Return oTestOBJ1.Name = oTestOBJ2.Name

    End Function

    Public Property Name() As String
        Get
            Return m_Name

        End Get
        Set(ByVal Value As String)
            m_Name = Value
        End Set
    End Property
End Class

It is my understanding that you have to implement the equals method on
the class being collected to get remove to work, as the documentation
explains that the remove method uses the equals method for the
comparison. Here is my code that is trying to remove an object from
the collection:

    Private Sub butRemove_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles butRemove.Click
        Dim otestobj, otestobj2 As TestOBJ
        'otestobj = New TestOBJ()
        'otestobj2 = New TestOBJ()
        'otestobj.Name = Me.txtName.Text
        'otestobj2.Name = "Pat"
        'MessageBox.Show(otestobj.equals(otestobj2))
        'MessageBox.Show(TestOBJ.equals(otestobj, otestobj2))

        oTestCol.Delete(Me.txtName.Text)
        Me.lstNames.Items.Clear()
        For Each otestobj In oTestCol

            Me.lstNames.Items.Add(otestobj.Name)
        Next
    End Sub

If I type "Pat" in the text box, and uncomment the commented out lines
above, both return TRUE, thus showing that my implementation of equals
is working. But, if I run the code, I get an item not found in
collection exception in the collection class' delete method.

Any ideas?

Thanks!



Tue, 05 Oct 2004 22:48:19 GMT  
 Collection Class Remove method
Pat,
You are creating a new object and asking the collection to remove the new
object. The new object does not exist in the collection!

Remove uses reference equality 'Is' to look for the object...

Unfortunately you will need to iterate the collection to find the object you
want to remove...

Which brings up. Why not use DictionaryBase as your base class? Other than
it appears that the name of your TestOBJ can change, which means you can't
really lookup and remove them from the collection anyway... What I mean is
if you use DictionaryBase, and put the object in the dictionary using one
name, then change the name of the object, the dictionary will have the wrong
name for lookup. Changing the name of the object, should notify the
dictionary so that the dictionary can update the key to the object...

Hope this helps
Jay


Quote:
> Hi. I am having a problem removing members from a collection class.
> Here's my code for the collection class, and the object being
> collected:

> Public Class TestCol
>     Inherits CollectionBase

>     Default Public Overloads ReadOnly Property Item(ByVal index As
> Integer) As TestOBJ
>         Get
>             Return CType(MyBase.List(index), TestOBJ)
>         End Get
>     End Property

>     Public Sub Add(ByVal Name As String)
>         Dim oTestOBJ As New TestOBJ()
>         oTestOBJ.Name = Name
>         MyBase.List.Add(oTestOBJ)

>     End Sub

>     Public Sub Delete(ByVal Name As String)
>         Dim oTestOBJ As New TestOBJ()
>         oTestOBJ.Name = Name
>         MyBase.List.Remove(oTestOBJ)

>     End Sub

> End Class

> Public Class TestOBJ
>     Private m_Name As String

>     Public Shadows Function equals(ByVal obj As Object) As Boolean
>         Dim oTestOBJ As TestOBJ
>         oTestOBJ = CType(obj, TestOBJ)
>         Return oTestOBJ.Name = m_Name

>     End Function

>     Public Shared Shadows Function equals(ByVal obj1 As Object, ByVal
> obj2 As Object) As Boolean
>         Dim oTestOBJ1, oTestOBJ2 As TestOBJ
>         oTestOBJ1 = CType(obj1, TestOBJ)
>         oTestOBJ2 = CType(obj2, TestOBJ)
>         Return oTestOBJ1.Name = oTestOBJ2.Name

>     End Function

>     Public Property Name() As String
>         Get
>             Return m_Name

>         End Get
>         Set(ByVal Value As String)
>             m_Name = Value
>         End Set
>     End Property
> End Class

> It is my understanding that you have to implement the equals method on
> the class being collected to get remove to work, as the documentation
> explains that the remove method uses the equals method for the
> comparison. Here is my code that is trying to remove an object from
> the collection:

>     Private Sub butRemove_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles butRemove.Click
>         Dim otestobj, otestobj2 As TestOBJ
>         'otestobj = New TestOBJ()
>         'otestobj2 = New TestOBJ()
>         'otestobj.Name = Me.txtName.Text
>         'otestobj2.Name = "Pat"
>         'MessageBox.Show(otestobj.equals(otestobj2))
>         'MessageBox.Show(TestOBJ.equals(otestobj, otestobj2))

>         oTestCol.Delete(Me.txtName.Text)
>         Me.lstNames.Items.Clear()
>         For Each otestobj In oTestCol

>             Me.lstNames.Items.Add(otestobj.Name)
>         Next
>     End Sub

> If I type "Pat" in the text box, and uncomment the commented out lines
> above, both return TRUE, thus showing that my implementation of equals
> is working. But, if I run the code, I get an item not found in
> collection exception in the collection class' delete method.

> Any ideas?

> Thanks!



Wed, 06 Oct 2004 07:58:57 GMT  
 Collection Class Remove method

Quote:
> Pat,
> You are creating a new object and asking the collection to remove the
> new object. The new object does not exist in the collection!

That's not the problem. Removes call the non-share Equals method of the
object to check if the objects or an equal object is on the collection. The
here problem is that the Equals is hidding the Object implementation and not
overriding it so it has four Equals methods (2 in TestOBJ and 2 in the base
class Object).

He has to change this:

Public Shadows Function equals(ByVal obj As Object) As Boolean

With this:

Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

To override the Object implementation of the method (remember that Remove
calls Equals on an Object not on a TestOBJ).

--
Eduardo A. Morcillo (MS MVP VB)
http://www.domaindlx.com/e_morcillo



Wed, 06 Oct 2004 12:44:39 GMT  
 Collection Class Remove method
Eduardo,
Doh!

I missed the Shadows keyword in there...

Now to find that test from a few months back where the Equals was not
behaving for me... I know it was not because I Shadowed the Equals
override...

Thanks for the correction...
Jay



Quote:

> > Pat,
> > You are creating a new object and asking the collection to remove the
> > new object. The new object does not exist in the collection!

> That's not the problem. Removes call the non-share Equals method of the
> object to check if the objects or an equal object is on the collection.
The
> here problem is that the Equals is hidding the Object implementation and
not
> overriding it so it has four Equals methods (2 in TestOBJ and 2 in the
base
> class Object).

> He has to change this:

> Public Shadows Function equals(ByVal obj As Object) As Boolean

> With this:

> Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

> To override the Object implementation of the method (remember that Remove
> calls Equals on an Object not on a TestOBJ).

> --
> Eduardo A. Morcillo (MS MVP VB)
> http://www.domaindlx.com/e_morcillo



Thu, 07 Oct 2004 00:06:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Remove Interface Methods from class methods

2. DataGrid remove item error bind with own class inherit from collection

3. DataGrid remove item error bind with own class inherit from collection

4. Delete item from a collection class that has no Delete method

5. Using the sort method in custom collection classes

6. Using the Item Method in a Collection Class

7. Public Method in a class calling a private method in same class yields ByRef error

8. Collection of Collections of Classes

9. VB4 class collections with collections

10. Exposing Properties collection in custom classes as many MS Classes do

11. StatusBarPanel Collection containing panels based on different classes derived from StatusBarPanel class

12. Exposing a Properties collection in custom classes as many MS Classes do

 

 
Powered by phpBB® Forum Software