?
Solved

look for a dupe in a collection

Posted on 2013-12-11
8
Medium Priority
?
237 Views
Last Modified: 2013-12-11
I'm trying to see if there is a duplicate before I add it to the collection, but it's not catching it.  Here is what I have.


Dim arrSRDirectorsSub5 As New Collection    'temp collection for loop
.
.
.
.
'check for dupes and warn
                        If arrSRDirectorsSub5.Contains(strClearUser & "," & strTitle) Then
                            MsgBox("We got a dupe " & strClearUser & "," & strTitle)

                        Else
                            arrLowLevelKeepers.Add(strClearUser & "," & strTitle)
                        End If
0
Comment
Question by:chadmanvb
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39711754
Are you sure this is VB.NET and not classic VB?
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39711757
Where is your loop? Is the collection declaration inside the loop?!?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39711834
Is this line of code, Dim arrSRDirectorsSub5 As New Collection, correct or is it something more like this, Dim arrSRDirectorsSub5 As New Collection(Of String)?

If it is a collection of strings you need to make sure that all characters are in the same case and that the strings are NOT in mix case with each otherwise they will not be found with Contains method. If this is the case use the ToUpper or To Lower to put both strings in the same case for comparison.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 

Author Comment

by:chadmanvb
ID: 39711847
I'm using .net 4.0.  The collection is decleared outside of the loop, but a function in the loop fills the collection.
0
 

Author Comment

by:chadmanvb
ID: 39711944
Opps, I did have a typo, but have the same issue not finding the adds

'check for dupes and warn
Dim arrLowLevelKeepers As New Collection
                        If arrLowLevelKeepers.Contains(strClearUser & "," & strTitle) Then

                            MsgBox("We got a dupe " & strClearUser & "," & strTitle)

                        Else
                            arrLowLevelKeepers.Add(strClearUser & "," & strTitle)
                        End If

It keeps adding arrLowLevelKeepers.Add(strClearUser & "," & strTitle) when there is a duplicate entry.  The loop does pass a few times, but on the thrid pass I have duplicates which I expect, but dont want to add them.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39712271
The reason I ask is because Collection is a classic VB type. I would suggest replacing it with one of the more modern .NET types.

See http://msdn.microsoft.com/en-us/library/ybcx56wz.aspx 
http://msdn.microsoft.com/en-us/library/0ytkdh4s(v=vs.110).aspx

Since you are trying to avoid duplicates a sensible choice would be a HashSet (which only allows one copy of each item) - essentially it manages everything for you.

More info here: http://msdn.microsoft.com/en-us/library/bb397727(v=vs.110).aspx

The final code is trivial!
Dim arrLowLevelKeepers As New HashSet(Of String)()
arrLowLevelKeepers.Add(strClearUser & "," & strTitle)

Open in new window

0
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 2000 total points
ID: 39712279
I honestly can't see why your code doesn't but I would suggest swapping the VB specific type out might solve it. So if you do want to respond to the duplicate instead of just ignoring them then I'd suggest using the List(Of String) type:
Dim arrLowLevelKeepers As New List(Of String)()
Dim itemToAdd As String = strClearUser & "," & strTitle
If arrLowLevelKeepers.Contains(itemToAdd) Then
    ' We have a dupe!
Else
    arrLowLevelKeepers.Add(itemToAdd)
End If

Open in new window

0
 

Author Closing Comment

by:chadmanvb
ID: 39712482
Thanks and that worked great!  I also like the hash idea.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses
Course of the Month6 days, 22 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question