ObjectCollections?!?

absent

Well-known member
Joined
Sep 25, 2003
Messages
55
Location
UK
Hey all... Im trying to use an object collection to try and compare variables in a listbox... The thing is when a button is clicked and the variable in the 1st listbox is seen as a duplicate no matter if there is the same value present or not...

Code:
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

        Dim cache As New ListDictionary

         Over here, the code to detect a click. Then...
        If Not cache.Contains(lstSelected.SelectedIndex) Then
            cache.Add(lstSelected.SelectedIndex, "")  Note that the value is irrelevant...were only interested in the key.
        Else
            Throw New Exception    Do whatever to handle this.
        End If

         Carry on with whatever...

        Try
            FoundMatch()
            If Not cache.Contains(lstSelected.SelectedIndex) Then
                MsgBox("Duplicate Entry", MsgBoxStyle.OKOnly, "Duplication Error")
                lstSelected.Items.Add(lstSelect.Text)
                lstSelect.Items.Remove(lstSelect.Text)
            ElseIf cache.Contains(lstSelected.SelectedIndex) Then
                lstSelected.Items.Add(lstSelect.Text)
                lstSelect.Items.Remove(lstSelect.Text)
                MsgBox("Duplicate Entry", MsgBoxStyle.OKOnly, "Duplication Error")
            End If

        Catch ex As Exception
            MsgBox("Error Type : " & Err.Description, MsgBoxStyle.Exclamation, "Error Number : " & Err.Number)
        End Try

    End Sub


Even if I comment/uncomment the values in the if block it still does not accept a value - Even if it is not present in the listbox...

Any ideas where I am going wrong? or another way of achieving my goal?
 
What?

Did you mean to do this:

[VB]

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

If (lstSelect.Items().Contains(lstSelected.SelectedItem())) Then
dup
Else
lstSelect.Items().Add(lstSelected.SelectedItem())
End If

End Sub



[/VB]
 
not quite....

Im using the ListDirectory class of the ObjectCollections... Anyone have an idea of how to use them as a string storage by using variables?

I have got the varialbes stored in a container called "cache" if the value is not present in the cache then it is added to the cache and added to a list box. If the value is already present in the cache then a msgbox pops up with a duplication error.

Is there a problem with my code? Or is my logic a little warped at the moment?

ta in advance
 
well, ya
caches lifetime is only in the Subroutine. Everytime you exit the sub the variable is deallocated from memory.
 
Right sorted it all out... I had to add 2 classes to achieve the desired result

Code:
Public Class cWidget
    Public Name As String
End Class

and

Code:
Public Class cWidgetCollection
    Inherits System.Collections.CollectionBase
     Visual Basic
     Creates a WidgetCollection object.
    Dim myWidgetCollection As New cWidgetCollection


     Restricts to Widget types, items that can be added to the collection.
    Public Sub Add(ByVal awidget As cWidget)
         Invokes Add method of the List object to add a widget.
        List.Add(awidget)
    End Sub

    Public Sub Remove(ByVal index As Integer)
         Check to see if there is a widget at the supplied index.
        If index > Count - 1 Or index < 0 Then
             If no widget exists, a messagebox is shown and the operation is 
             cancelled.
            System.Windows.Forms.MessageBox.Show("Index not valid!")
        Else
             Invokes the RemoveAt method of the List object.
            List.RemoveAt(index)
        End If
    End Sub

     This line declares the Item property as ReadOnly, and 
     declares that it will return a Widget object.
    Public ReadOnly Property Item(ByVal index As Integer) As cWidget
        Get
             The appropriate item is retrieved from the List object and 
             explicitly cast to the Widget type, then returned to the 
             caller.
            Return CType(List.Item(index), cWidget)
        End Get
    End Property

End Class

then in the button click event I put

Code:
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

        Dim i As Integer
        For i = 0 To myWidgetCollection.Count - 1

            If lstSelect.Text = myWidgetCollection.Item(i).Name Then
                MsgBox("Duplicate Record" & " " & myWidgetCollection.Item(i).Name _
                , MsgBoxStyle.OKOnly, "Duplicate Record Detected")
                Exit Sub
            End If
        Next
end sub

It all works fine and can be used for form lifetime collections instead of single instance lifetimes.... If anyone has a comment or easier way to do this please let me know...

Ta
 
Back
Top