How would I compare two Lists(Of <CustomClass>) in VB?
Posted
by
Kumba
on Stack Overflow
See other posts from Stack Overflow
or by Kumba
Published on 2011-01-16T04:48:17Z
Indexed on
2011/01/16
4:53 UTC
Read the original article
Hit count: 196
I'm working on implementing the equality operator =
for a custom class of mine. The class has one property, Value
, which is itself a List(Of OtherClass)
, where OtherClass
is yet another custom class in my project.
I've already implemented the IComparer
, IComparable
, IEqualityComparer
, and IEquatable
interfaces, the operators =
, <>
, bool
and not
, and overriden Equals
and GetHashCode
for OtherClass
. This should give me all the tools I need to compare these objects, and various tests comparing two singular instances of these objects so far checks out.
However, I'm not sure how to approach this when they are in a List
. I don't care about the list order. Given:
Dim x As New List(Of OtherClass) From
{New OtherClass("foo"),
New OtherClass("bar"),
New OtherClass("baz")}
Dim y As New List(Of OtherClass) From
{New OtherClass("baz"),
New OtherClass("foo"),
New OtherClass("bar")}
Then (x = y).ToString
should print out True
.
I need to compare the same (not distinct) set of objects in this list. The list shouldn't support dupes of OtherClass
, but I'll have to figure out how to add that in later as an exception. Not interested in using LINQ. It looks nice, but in the few examples I've played with, adds a performance overhead in that bugs me. Loops are ugly, but they are fast :)
A straight code answer is fine, but I'd like to understand the logic needed for such a comparison as well. I'm probably going to have to implement said logic more than a few times down the road.
© Stack Overflow or respective owner