'ArrayList.Contains Always Returns False (Checking for an Object)

I'm trying to utilize an ArrayList in VBA (though it's the same ArrayList as available in C# and VB.net). When checking if an object is in the array list, the output is always false.

Example checking if "anotherPart" already exists in "partsList". Here it should already exist, because an identical "testPart" has already been added:

Dim partsList As Object
Set partsList = CreateObject("System.Collections.ArrayList")

Dim testPart As New part
testPart.Number = "10"

partsList.Add testPart

Dim anotherPart As New part
anotherPart.Number = "10"

Debug.Print ("partsList contains: " & partsList.Contains(anotherPart))

Debug output:

partsList contains: False

Why won't partsList.Contains(anotherPart) return true???

testPart and anotherPart are different parts, but they are identical, being defined with number="10". partsList.Contains(testPart) returns true, but I don't understand why partsList.Contains(anotherPart) returns false if anotherPart is the same as testPart?

Edit to clarify purpose: The reason I'm asking this, is I want to check if anotherPart already exists in the list, and only add it if an identical entry is not present. I'd expected to be able to do this, but can't because contains always returns false, and I end up adding tons of duplicate parts:

If Not partList.Contains(anotherPart) Then
  partsList.Add anotherPart
End If

This seemed to show this as being possible, though they are overwriting the same udtPerson each time. Is there not a way to check this if using a "new" part and not overwriting an existing part? If I'm comparing one list against another for example each object will have been a "new" one.

http://www.java2s.com/Tutorial/VB/0160__Collections/UseArrayListContainstocheckifaobjectalreadyexists.htm



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source