Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Finding Dupe in a List

Reply
Thread Tools

Finding Dupe in a List

 
 
Larry Bud
Guest
Posts: n/a
 
      07-30-2007
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?

 
Reply With Quote
 
 
 
 
Omar Abid
Guest
Posts: n/a
 
      07-30-2007
On 30 juil, 15:26, Larry Bud <(E-Mail Removed)> 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
> "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?


Hi,
Check out this link : http://expressdotnet.freehostia.com
Omar Abid

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      07-31-2007
Larry Bud 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
> "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?
>


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.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Larry Bud
Guest
Posts: n/a
 
      07-31-2007
On Jul 31, 5:56 am, Göran Andersson <(E-Mail Removed)> wrote:
> Larry Bud 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
> > "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?

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


Ok, I get it. However, from what I see, I can't sort a Dictionary.



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      07-31-2007
Larry Bud wrote:
> On Jul 31, 5:56 am, Göran Andersson <(E-Mail Removed)> wrote:
>> Larry Bud 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
>>> "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?

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

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

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
Firefox (mybe a dupe) MrMagoo Computer Support 2 12-19-2005 11:54 PM
dupe records on refresh 2obvious ASP General 1 02-11-2005 05:35 PM
*HELP* please: how to dupe STDIN in an IO::Scalar? J Krugman Perl Misc 1 11-25-2004 01:53 AM
Finding new / dupe files on large file system Anonymous Coward Computer Support 2 09-25-2004 05:19 AM



Advertisments