Search code examples
.netvb.netmoduleequalityshadowing

How to shadow Object.Equals() method in a Module?


Maybe the title of the question is not the proper, because the methods in a module cannot be shadowed, but In essence I want to shadow a base member declaring it again, and hidding it without loosing the opportunity to call it and return the proper value.

Normally I do this for Classes:

<EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function Equals(ByVal obj As Object) As Boolean
    Return MyBase.Equals(obj)
End Function

And this for Structures:

<EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Function Equals(ByVal obj As Object) As Boolean
    Return Object.Equals(obj, Me)
End Function

The problem I find trying to reproduce this behavior with a module, is what I should use to fill the interrogant argument in the example below, since I can't use Me, the module name, or a constructor, I understand it, but what I need to use then?.

Module TestModule

    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Function Equals(ByVal obj As Object) As Boolean
        Return Object.Equals(obj, ?)
    End Function

End Module

Solution

  • This function, defined in Object, seems to be the one you are trying to shadow:

    Public Overridable Function Equals (obj As Object) As Boolean
    

    However this doesn't exist in a module. The "Equals" you are seeing Intellisense suggest after your module name is the shared one:

    Public Shared Function Equals (objA As Object, objB As Object) As Boolean
    

    You can see them listed separately in the documentation. To stop Intellisense suggesting it, just include this in your module:

    <EditorBrowsable(EditorBrowsableState.Never)> _
    Public Function Equals(objA As Object, objB As Object) As Boolean
        Return Object.Equals(objA, objB)
    End Function
    

    It doesn't seem good design though