Reputation: 9134
I want that each class in a certain namespace implement IEquatable(Of thatClass)
and its corresponding GetHashCode
.
Namespace DAO
Public Class Product : Implements IEquatable(Of Product)
Shadows Function Equals(ByVal other As Product) As Boolean Implements IEquatable(Of Product).Equals
Shadows Function GetHashCode() As Integer
End Class
End Namespace
I can just "remember" to implement the two methods but that would be poor practice.
I could write an interface IDao and again "remember" to let all the classes implement that interface. But an interface can't implement another interface.
Public Interface IDao : Implements IEquatable ' <---- NO!
Ok, so I could create an abstract class (bad, the Dao classes won't be able to inherit from other classes) and again remember to let all the classes inherit from it:
Public MustInherit Class DaoBase : Implements IEquatable(Of DaoBase)
MustOverride Shadows Function Equals(ByVal other As DaoBase) As Boolean Implements IEquatable(Of DaoBase).Equals
MustOverride Shadows Function GetHashCode() As Integer
End Class
Public Class Product
Inherits DaoBase
Overrides Function Equals(ByVal other As DaoBase) As Boolean
Return ???
End Function
Now the parameter in Equals is a DaoBase, not a Product. I could cast, but...
Or I could generalize the DaoBase like:
Public MustInherit Class DaoBase(Of T) : Implements IEquatable(Of T)
MustOverride Shadows Function Equals(ByVal other As T) As Boolean Implements IEquatable(Of T).Equals
MustOverride Shadows Function GetHashCode() As Integer
End Class
But that means a different DaoBase will be created for each inheriting class.
Is there a way to declare DaoBase non-generic and have the subclasses handle the right types?
Nice to have: force each class in a namespace to implement an interface by contract. So I don't have to remember ;)
Upvotes: 0
Views: 617
Reputation: 28747
An interface cannot implement another interface, but it can inherit from another interface. The following works:
Public Interface IDao
Inherits IEquatable
End Interface
So you could apply your initial thought with this approach
Upvotes: 1