Add Distinction to Your Code: VB: Implement a Comparer Class: Listing 1

A comparer class must implement IEquatable(Of T) and override the System.Object's or the base class' Equals(object) method and GetHashCode method. The GetHashCode method must return the same value for objects to be considered equal because it's the first method called in the equality test.

Class Location
    Implements IEquatable(Of Location)

    Public Property State() As String
    Public Property Suburb() As String
    Public Property PostCode() As Int32

    Public Overrides Function GetHashCode() As Int32
        Return PostCode.GetHashCode Xor _
            State.GetHashCode Xor _
            Suburb.GetHashCode
    End Function

    Public Overrides Function Equals( _
        ByVal obj As Object) As Boolean
        Return Equals(TryCast(obj, Location))
    End Function

    Public Overloads Function Equals( _
        ByVal other As Location) As Boolean _
        Implements IEquatable(Of Location).Equals
        If other Is Nothing Then Return False
        Return PostCode = other.PostCode _
            AndAlso State.Equals(other.State, _
            StringComparison.CurrentCultureIgnoreCase) _
            AndAlso Suburb.Equals(other.Suburb, _
            StringComparison.CurrentCultureIgnoreCase)
    End Function

End Class
comments powered by Disqus

Featured

  • Mastering AI Development and Building AI Apps with GitHub Copilot

    Two Microsoft experts explain how GitHub Copilot is evolving from a coding assistant into a broader platform for building, customizing and testing AI-powered developer workflows.

  • VS Code 1.123 Adds Agent Session Sync, 1M Context Windows

    Microsoft released Visual Studio Code 1.123 on June 3, adding agent-focused features, larger model context support, integrated browser updates and a new delay for some automatic extension updates.

  • Copilot Billing Shock Hits Developers

    Developer complaints about GitHub Copilot's new usage-based billing model have centered on unexpectedly rapid AI credit consumption, and neither GitHub nor Microsoft has responded directly to the backlash, though they have previously published guidance to lessen model usage costs.

  • Hands On with GitHub Copilot App Technical Preview: Turning a Blazor Issue into a PR

    GitHub's brand-new Copilot desktop app, in technical preview, handled a small Blazor issue from planning through pull request creation, but the hands-on test also showed why developers still need to verify agent work in the running app before merging.

Subscribe on YouTube