Finding Dupe in a List

Discussion in 'ASP .Net' started by Larry Bud, Jul 30, 2007.

  1. Larry Bud

    Larry Bud Guest

    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?
     
    Larry Bud, Jul 30, 2007
    #1
    1. Advertising

  2. Larry Bud

    Omar Abid Guest

    On 30 juil, 15:26, 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?


    Hi,
    Check out this link : http://expressdotnet.freehostia.com
    Omar Abid
     
    Omar Abid, Jul 30, 2007
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jul 31, 2007
    #3
  4. Larry Bud

    Larry Bud Guest

    On Jul 31, 5:56 am, Göran Andersson <> 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.
     
    Larry Bud, Jul 31, 2007
    #4
  5. Larry Bud wrote:
    > On Jul 31, 5:56 am, Göran Andersson <> 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
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jul 31, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    788
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    504
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    720
    Debajit Adhikary
    Oct 18, 2007
  4. 2obvious

    dupe records on refresh

    2obvious, Feb 11, 2005, in forum: ASP General
    Replies:
    1
    Views:
    117
    Bob Barrows [MVP]
    Feb 11, 2005
  5. J Krugman
    Replies:
    1
    Views:
    110
    KKramsch
    Nov 25, 2004
Loading...

Share This Page