Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problems with List(of T).Contains method

Problems with List(of T).Contains method

Scheduled Pinned Locked Moved Visual Basic
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dilkonika
    wrote on last edited by
    #1

    Hello! I have this Code :

    Friend Class MyTestClass
    Public Property nr As Integer
    Public Property name As String
    End Class

    Dim mytest As New List(Of MyTestClass)

    Dim k as integer=2

    Dim s as string="123"

    Private sub1()

     Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s}
    
     mytest.Add(item)
    
     Messagebox.show(mytest.contains(item))
    

    End sub

    Private Sub2 ()
    Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s)

      Messagebox.show(mytest.contains(item))
    

    End sub

    Using a button I run first Sub1 , and Messagebox display the "True" After using another button I run the Sub 2 , and the messagebox display "False". But should display "True" Why ? What can I do to get the correct result ? Thank you !

    P B 2 Replies Last reply
    0
    • D dilkonika

      Hello! I have this Code :

      Friend Class MyTestClass
      Public Property nr As Integer
      Public Property name As String
      End Class

      Dim mytest As New List(Of MyTestClass)

      Dim k as integer=2

      Dim s as string="123"

      Private sub1()

       Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s}
      
       mytest.Add(item)
      
       Messagebox.show(mytest.contains(item))
      

      End sub

      Private Sub2 ()
      Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s)

        Messagebox.show(mytest.contains(item))
      

      End sub

      Using a button I run first Sub1 , and Messagebox display the "True" After using another button I run the Sub 2 , and the messagebox display "False". But should display "True" Why ? What can I do to get the correct result ? Thank you !

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You are creating two items wit the same value, but they are still two separate items and therefore not Equal. You could define a custom Equals method for the class.

      D 1 Reply Last reply
      0
      • P PIEBALDconsult

        You are creating two items wit the same value, but they are still two separate items and therefore not Equal. You could define a custom Equals method for the class.

        D Offline
        D Offline
        dilkonika
        wrote on last edited by
        #3

        How ?

        P 1 Reply Last reply
        0
        • D dilkonika

          How ?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          http://msdn.microsoft.com/en-us/library/ms131187(v=vs.110).aspx[^]

          1 Reply Last reply
          0
          • D dilkonika

            Hello! I have this Code :

            Friend Class MyTestClass
            Public Property nr As Integer
            Public Property name As String
            End Class

            Dim mytest As New List(Of MyTestClass)

            Dim k as integer=2

            Dim s as string="123"

            Private sub1()

             Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s}
            
             mytest.Add(item)
            
             Messagebox.show(mytest.contains(item))
            

            End sub

            Private Sub2 ()
            Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s)

              Messagebox.show(mytest.contains(item))
            

            End sub

            Using a button I run first Sub1 , and Messagebox display the "True" After using another button I run the Sub 2 , and the messagebox display "False". But should display "True" Why ? What can I do to get the correct result ? Thank you !

            B Offline
            B Offline
            bojammis
            wrote on last edited by
            #5

            Howdy, In Sub2() you have defined a new MyTestClass but you have not added it to the list. The item defined in sub1 is scoped to sub1 and exists because it was added to the list. The item in sub2 is scoped to sub2 but was not asdded to the list. You are asking the list to check for an item (defined and scoped to sub2) that has not been added to the list yet.

            Private Sub2 ()
            Dim item As MyTestClass = New MyTestClass With {.nr = k, .FieldName =s)

            mytest.add(item)   'Add this line
            
            Messagebox.show(mytest.contains(item))
            

            End sub

            regs.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups