Results 1 to 6 of 6

Thread: [RESOLVED] Check If a Collection contains element

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2008
    Location
    South Africa
    Posts
    395

    Resolved [RESOLVED] Check If a Collection contains element

    I want to take a field from a table and place the content into a collection. The collection is not supposed to have any duplicates. This is my code sofar... It does not work. The NOT statement is supposed to only enter values into the collection, when the collection does not allready contain the matching string.

    Code:
    dim finalgroup as new collection
    Dim stu As New dbase("student")
            dt = New DataTable
            ds = New DataSet
            sqlString = "select  studentclass from student"
            adp = New OleDbDataAdapter(sqlString, con)
            adp.Fill(dt)
            adp.Fill(ds)
            Dim dr As DataRow
            For Each dr In ds.Tables(0).Rows()
                group = dr.Item(0).ToString
                If Not finalgroup.Contains(group) Then
                    finalgroup.Add(group)
                End If
            Next

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check If a Collection contains element

    Please don't just say "It does not work"

    Does it compile?
    Does it throw a run-time exception?
    Does it run through your code but not do what you expect?

    If you narrow it down then people will be more prepared to help.

    The simplest way of achieving what you want is to change your SQL query to return only unique rows, that way you don't even need to test :

    Code:
    sqlString = "SELECT DISTINCT studentclass FROM student"

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check If a Collection contains element

    The reason your code doesn't work at the moment (ie the check to see if an item exists it will always return false) is because the contains method checks for an item with the specified key and you aren't setting a key for the items you add.

    If you change this

    Code:
    finalgroup.Add(group)
    to

    Code:
    finalgroup.Add(group, group)
    it should work, but like I say you don't even need the check for duplicates if you simply amend your SQL query to return only unique values.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2008
    Location
    South Africa
    Posts
    395

    Re: Check If a Collection contains element

    I did compile it.. That works,
    It does not throw an exception...
    ....

    ...But your answer did help me... I am not good with SQL

    But just to make sure.. HOW will i check a collection for duplicates??

    Thanks.

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check If a Collection contains element

    Quote Originally Posted by marius40 View Post
    I did compile it.. That works,
    It does not throw an exception...
    ....

    ...But your answer did help me... I am not good with SQL

    But just to make sure.. HOW will i check a collection for duplicates??

    Thanks.
    See my 2nd post above... your code won't work because you aren't setting the key for the item as you add it to the collection. the "Contains" method checks for the existence of an item with the specified key. It doesn't check the text of whats in the collection.

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Check If a Collection contains element

    Why use a collection at all, why not a generic List(Of String)? Then the Contains method will work as expected.

    If you're using this 'duplicate check' loads of times in different places, you might also want to consider inheriting the System.Collections.ObjectModel.Collection(Of String), and overriding the InsertItem method, and do your duplicate check there. That way you can simply call the Add method as usual (without the check) and it will do the checking for you.

    vb.net Code:
    1. Public Class StudentGroupCollection
    2.    Inherits System.Collections.ObjectModel.Collection(Of String)
    3.  
    4.    Protected Overrides InsertItem(ByVal item As String, ByVal index As Integer)
    5.       If Not Me.Contains(item) Then
    6.          MyBase.Insert(item, index)
    7.       End If
    8.    End Sub
    9.  
    10. End Class
    11.  
    12.  
    13.  
    14. 'usage:
    15. Dim col As New StudentGroupCollection
    16. col.Add("ABC")
    17. col.Add("DEF")
    18. col.Add("ABC")
    19.  
    20. 'col should contain only 2 items.

    Note, this is typed here in the browser; there might be a few mistakes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width