0

I have the following class based on example code from Microsoft MSDN:

Imports System.Collections.Generic
Module SharedCode

    Public Class Fund
        Implements IEquatable(Of Fund)

    'Class Fund must implement Function Equals(other As RetirementCalcOverTime.SharedCode.Fund) As Boolean for interface System.IEquatable(Of Fund)

        Public Property FundName As String
        Public Property StartDate As Date
        Public Property StartBalance As Double
        Public Property StartQuantity As Double
        Public Property StartPrice As String

        Public Sub New()
        End Sub
        Public Sub New(ByVal sFundName As String,
                                     ByVal dStartDate As Date,
                                     ByVal pStartBalance As Double,
                                     ByVal pStartQuantity As Double,
                                     ByVal pStartPrice As Double)
            FundName = sFundName
            StartDate = dStartDate
            StartBalance = pStartBalance
            StartQuantity = pStartQuantity
            StartPrice = pStartPrice
        End Sub
        Public Function Overrides Equals(ByVal obj As Fund) As Boolean 

        'Overrides is flagged as invalid identifier

            If obj Is Nothing Then
                Return False
            End If
            Dim objAsFund As Fund = TryCast(obj, Fund)
            If objAsFund Is Nothing Then
                Return False
            Else
                Return Equals(objAsFund)
            End If
        End Function

    End Class

End Module

What am I doing wrong that overrides and the Equals function throw errors?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
twellsles
  • 1
  • 2

2 Answers2

0

You need two functions:

Public Function Overrides Equals(ByVal obj As Object) As Boolean

to override Object.Equals, and

Public Function Equals(ByVal obj As Fund) As Boolean Implements IEquatable(Of Fund).Equals

to implement the interface method.

Your current implementation is appropriate for the first method (since you check to see if it is a Fund) provided you change the parameter type to Object - now you just need the second method to define "equality" for two Fund objects, which will satisfy the interface implementation.

You should also override GetHashCode to be consistent with your equality definition (two "equal" objects must return equal hash codes).

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

the problem is in Equals :

 Public Overrides Function Equals(obj As Fund) As Boolean
     If obj Is Nothing Then
         Return False
     End If
     Dim objAsFund As Fund = TryCast(obj, Fund)
     If objAsFund Is Nothing Then
         Return False
     Else
        If Me.FundName == objAsFund.FundName AndAlso ...
     End If
 End Function

you should put your condition (for example all properties are the same) instead of '...'

lordkian
  • 95
  • 7
  • Hitting the enter key after the line: Implements IEquatable(Of Fund) adds the following: Public Function Equals1(other As Fund) As Boolean Implements IEquatable(Of Fund).Equals End Function I must be kind of slow because I have no idea what to do with this because MSDN doesn't mention it and says to override Equals. What am I missing here? – twellsles Dec 07 '16 at 17:46