Problems with List(of T).Contains method
-
Hello! I have this Code :
Friend Class MyTestClass
Public Property nr As Integer
Public Property name As String
End ClassDim 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 !
-
Hello! I have this Code :
Friend Class MyTestClass
Public Property nr As Integer
Public Property name As String
End ClassDim 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 !
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.
-
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.
-
-
Hello! I have this Code :
Friend Class MyTestClass
Public Property nr As Integer
Public Property name As String
End ClassDim 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 !
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.