|
-
Jan 30th, 2010, 05:56 AM
#1
Thread Starter
Hyperactive Member
[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
-
Jan 30th, 2010, 07:09 AM
#2
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"
-
Jan 30th, 2010, 07:31 AM
#3
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.
-
Jan 30th, 2010, 07:40 AM
#4
Thread Starter
Hyperactive Member
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.
-
Jan 30th, 2010, 07:54 AM
#5
Re: Check If a Collection contains element
 Originally Posted by marius40
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.
-
Jan 30th, 2010, 10:57 AM
#6
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:
Public Class StudentGroupCollection Inherits System.Collections.ObjectModel.Collection(Of String) Protected Overrides InsertItem(ByVal item As String, ByVal index As Integer) If Not Me.Contains(item) Then MyBase.Insert(item, index) End If End Sub End Class 'usage: Dim col As New StudentGroupCollection col.Add("ABC") col.Add("DEF") col.Add("ABC") 'col should contain only 2 items.
Note, this is typed here in the browser; there might be a few mistakes.
Last edited by NickThissen; Jan 30th, 2010 at 11:00 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|