Solved

look for a dupe in a collection

Posted on 2013-12-11
8
224 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

16 Experts available now in Live!

Get 1:1 Help Now