Solved

look for a dupe in a collection

Posted on 2013-12-11
8
225 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 62

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now