?
Solved

look for a dupe in a collection

Posted on 2013-12-11
8
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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