vb.net - Implement IEquatable Get distinct objects -


this not working me. couldn't find answer on msdn or elsewhere after having spent time on it. missing?

public class printerinfo     implements iequatable(of printerinfo)     public printername string     public printerdesc string      'default equality comparer class vb.net     public overloads function equals(byval other printerinfo) boolean _        implements iequatable(of printerinfo).equals         return other.printername = me.printername     end function  end class  public readonly property printerinfolist(byval normal normalcopier) list(of printerinfo)             dim plist1 list(of printerinfo) = getlist         plist1.sort()         return plist1.distinct.tolist     end end property 

i list fine want distinct items. tried implement equality comparer it's not working. i'm getting multiple duplicates. need distinct values?

msdn: enumerable.distinct(of tsource)

msdn: iequalitycomparer(of t) interface

this seems similar don't understand it

i'd avoid linq groupby if can. seems clumsy me.

the documentation enumerable.distinct(of source) says:

the default equality comparer, default, used compare values of types implement iequatable<t> generic interface. compare custom data type, need implement interface and provide own gethashcode , equals methods type.

that's part you're missing. expected provide gethashcode() implementation in class. if @ code examples given, you'll see there too. , when think it, makes sense. implementation of distinct uses hash set internally, naturally requires proper gethashcode implementation function properly.

in case, try adding printerinfo class:

public overrides function gethashcode() integer     return me.printername.gethashcode() end function  

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -