Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

Finding Dupe in a List

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Larry Bud - 30 Jul 2007 14:26 GMT
I have a function which return a List, and I'm trying to add users to
it from 3 different roles, but I don't want duplicate user names.  The
"Contains" ALWAYS is returning FALSE, so I'm not finding the dupe.
Here's the code:

Dim datalist As New List(Of TroubleTicketUser)
Dim str As String
Dim theroles(2) As String, therole As String

theroles(0) = "IT User"
theroles(1) = "IT Admin"
theroles(2) = "Customer Service Rep"

For Each therole In theroles
    For Each str In Roles.GetUsersInRole(therole)
        Dim tt As New TroubleTicketUser(str)
        If Not datalist.Contains(tt) Then datalist.Add(tt)
    Next
Next

datalist.Sort()

Return datalist

---------------------
Why doesn't the Contains work?
Omar Abid - 30 Jul 2007 16:41 GMT
> I have a function which return a List, and I'm trying to add users to
> it from 3 different roles, but I don't want duplicate user names.  The
[quoted text clipped - 22 lines]
> ---------------------
> Why doesn't the Contains work?

Hi,
Check out this link : http://expressdotnet.freehostia.com
Omar Abid
Göran Andersson - 31 Jul 2007 10:56 GMT
> I have a function which return a List, and I'm trying to add users to
> it from 3 different roles, but I don't want duplicate user names.  The
[quoted text clipped - 22 lines]
> ---------------------
> Why doesn't the Contains work?

The Contains method works just fine, it just doesn't work the way that
you expected it to work.

The Contains method, when used on a List of reference types, checks if a
reference to the object exists in the list. As you create a new instance
of the TroubleTicketUser class, it's not the same object as the one that
exists in the list, eventhough they contain the same data.

What you want to do is to use a Dictionary(Of String, TroubleTicketUser)
where you use the user name as key.

Signature

Göran Andersson
_____
http://www.guffa.com

Larry Bud - 31 Jul 2007 14:10 GMT
On Jul 31, 5:56 am, G?ran Andersson <gu...@guffa.com> wrote:
> > I have a function which return a List, and I'm trying to add users to
> > it from 3 different roles, but I don't want duplicate user names.  The
[quoted text clipped - 33 lines]
> What you want to do is to use a Dictionary(Of String, TroubleTicketUser)
> where you use the user name as key.

Ok, I get it.  However, from what I see, I can't sort a Dictionary.
Göran Andersson - 31 Jul 2007 21:36 GMT
>>> I have a function which return a List, and I'm trying to add users to
>>> it from 3 different roles, but I don't want duplicate user names.  The
[quoted text clipped - 28 lines]
>
> Ok, I get it.  However, from what I see, I can't sort a Dictionary.

That is correct. You have to use an Array, a List or a SortedList for
that (or perhaps there are some more collections that can be sorted).

If you use a SortedList for example, you just have to loop through the
Values of the dictionary and add them to the list.

Signature

Göran Andersson
_____
http://www.guffa.com


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.