Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

look for a dupe in a collection

Posted on 2013-12-11
8
Medium Priority
?
236 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

773 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