Subject: Contains method not working in items collection
Posted By: jonk Post Date: 11/14/2004 7:52:54 AM
I can not get the contains method to work - in the collection of objects I have in a list box. Is these a certain method I have to support in my object to make the contains method work?


Below is the code I am using to:
1) insert objects into the list (with the broken contains method)
.....[If Not lstDest.Items.Contains(objRef) Then <-- IS BROKEN]
2) the class definition for the objects I am adding


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles btnAdd.Click, lstSource.DoubleClick
        ' Sunday, Nov 14, 2004: had to modify this routine to not add actUgrp's that are
        '   already in the dest list....
        '
        Dim intRelsAdded As Integer = 0
        Dim objRef As uGrpBaseRow
        Try
            Cursor.Current = Cursors.WaitCursor
            If lstSource.SelectedItems.Count > 1 Then lstDest.BeginUpdate()
            For Each objRef In lstSource.SelectedItems  ' 1 or more baseUrows can be sel'ed
                If Not lstDest.Items.Contains(objRef) Then
                    lstDest.Items.Add(objRef)
                    intRelsAdded += 1
                End If
            Next
            If lstSource.SelectedItems.Count > 1 Then lstDest.EndUpdate()
            While lstSource.SelectedIndices.Count > 0
                'objRef =
                lstSource.Items.RemoveAt(lstSource.SelectedIndices(0))
            End While
            updateListCounts()
            RaiseEvent destListChanged_viaInternalBtn("Added (" & intRelsAdded & ") " & _
                                                    "relations to the current grouping...")
        Catch ex As Exception
                mObj4errhandling.writeError2log(ex)
                Throw New Exception(mObj4errhandling.seeEventLogErrNotice, ex)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

2) the class definition for the objects I am adding
Here's the objects I am adding to the list's items collection (they contain just a name
and a (unique) dbID:

Private Class uGrpBaseRow
        Inherits Object
        ' these sp names are for the user group table         
Private mStrBaseRowName As String
        Private mIntBaseRowID As Integer

        Public Sub New(ByVal strBaseRowName As String, _
                        ByVal strBaseRowID As Integer)
            MyBase.new()
            mStrBaseRowName = strBaseRowName
            mIntBaseRowID = strBaseRowID
        End Sub
        ' need this method so the obj will display in the combo box..
        Public Overrides Function toString() As String
            Return mStrBaseRowName
        End Function
        Public ReadOnly Property name() As String
            Get
                Return mStrBaseRowName
            End Get
        End Property
        Public ReadOnly Property id() As Integer
            Get
                Return mIntBaseRowID
            End Get
        End Property
    End Class
#End Region


Go to topic 21607

Return to index page 717
Return to index page 716
Return to index page 715
Return to index page 714
Return to index page 713
Return to index page 712
Return to index page 711
Return to index page 710
Return to index page 709
Return to index page 708