I am having a CA1067 violation on IEquatable(Of T) and can't solve it

121 Views Asked by At

I have been trying for hours and a lot recode but can get rid of the CA1067 violation.
Using:
Visual Studio 2022, .Net v6.0.13, VB.NET

I will appreciate any help to solve the issue and insights in what I am doing wrong.

So the case is the following:
I have a template class SimNode(Of P,A) where P stands for the data type for parent and A for the data type of the three attributes of the node.

Public Class SimNode(Of P, A)
    Implements ISimNode(Of P, A)
    Implements IEquatable(Of SimNode(Of P, A))

    '[a bunch of properties and methods]

    Public Overridable Shadows Function Equals(other As SimNode(Of P, A)) As Boolean Implements IEquatable(Of SimNode(Of P, A)).Equals
        If Not Parent.Equals(other.Parent) Then Return False
        If Depth <> other.Depth Then Return False
        ....
        Return True
    End Function
End Class

I then needed to create another class called SimNode which inherits from SimNode(UShort,UShort) and requires an IEquatable(Of SimNode) because only unique SimNode instances will be added into a template 'container' -> Container(Of T as IEquatable(Of T)).
The word container is generic it could be e.g. a list, a dictionary or a hashset.

This new class is exactly the same as the parent class but with an extra member (list).

Private Class SimNode
    Inherits SimNode(Of UShort, UShort)
    Implements IEquatable(Of SimNode)

    '[a bunch of properties and methods]

    Private Shadows Function Equals(other As SimNode) As Boolean Implements IEquatable(Of SimNode).Equals
        Return MyBase.Equals(other)
    End Function
End Class

My equality criteria is still the same as the one in the parent class despite the extra list.
This approach is leading to a CA1067 violation and I just cannot get this correct.

I will appreciate very much any help!

I have try to follow the suggestions from Visual Studio but all lead to error. The suggestion of override the method Equals in the child class (SimNode) will produce obviously error because it can't override the base class since they have different signatures.

I also worked around this https://stackoverflow.com/questions/2441346/cascading-iequatableof-t with no success.

1

There are 1 best solutions below

7
Paulo Borges On

After all the great feedback I came up to the answer that solved the violation. The code is a bit out of how I would code but it seems that this is the correct way to do. Some feedback on the answer will be great to know if I did this correctly and if this is what you guys were trying to tell me! ;)

Module Program
    Sub Main()
        Dim storage As New Container(Of Y)
    End Sub

    Private Class Container(Of T As IEquatable(Of T))

    End Class

    Private Class X(Of P, A)
        Implements IEquatable(Of X(Of P, A))

        Public ReadOnly Parent As P
        Public ReadOnly Attribute As A

        Public Sub New(parent As P, attribute As A)
            Dim typ As Type : Dim datTyp As Integer
            Dim acceptingTypes As Integer() = {6, 8, 10, 12} '{Byte, USHort, UInteger, ULong}
            'check the type of P.
            typ = GetType(P) : datTyp = Type.GetTypeCode(typ)
            If Not acceptingTypes.Contains(datTyp) Then Throw New ArgumentException("Type 'P' is not acceptable.", NameOf(P))
            'check the type of A.
            typ = GetType(A) : datTyp = Type.GetTypeCode(typ)
            If Not acceptingTypes.Contains(datTyp) Then Throw New ArgumentException("Type 'A' is not acceptable.", NameOf(A))

            Me.Parent = parent : Me.Attribute = attribute
        End Sub

        Public Overridable Function IEquatable_Equals(other As X(Of P, A)) As Boolean Implements IEquatable(Of X(Of P, A)).Equals
            If Not Parent.Equals(other.Parent) Then Return False
            If Not Attribute.Equals(other.Attribute) Then Return False

            Return True
        End Function

        Public Overrides Function Equals(obj As Object) As Boolean
            Return DirectCast(Me, IEquatable(Of X(Of P, A))).Equals(TryCast(obj, X(Of P, A)))
        End Function

        Public Overrides Function GetHashCode() As Integer
            Return Parent.GetHashCode() + Attribute.GetHashCode()
        End Function
    End Class

    Private Class Y
        Inherits X(Of UShort, UShort)
        Implements IEquatable(Of Y)

        Public ReadOnly Lines As List(Of Integer)

        Public Sub New(parent As UShort, attribute As UShort)
            MyBase.New(parent, attribute)
            Lines = New List(Of Integer)
        End Sub

        Public Overloads Function Equals(other As Y) As Boolean Implements IEquatable(Of Y).Equals
            Return MyBase.IEquatable_Equals(other)
        End Function

        Public Overrides Function Equals(obj As Object) As Boolean
            Return DirectCast(Me, IEquatable(Of Y)).Equals(TryCast(obj, Y))
        End Function

        Public Overrides Function GetHashCode() As Integer
            Return Parent + Attribute 'Or Mybase.GetHashCode()?
        End Function
    End Class
End Module