IObject: Equals

Equals

Determines whether the specified value is equal to the current IObject.



 Public Function Equals(
	  ByRef Value As Variant ) As Boolean

Parameters

Value
[ByRef] Variant. The value to compare the current IObject.

Return Values

Boolean -  True if the specified value is equal to the current IObject; otherwise, False.

Remarks

The Equals method is used to check for equality between two objects. It is up to each object to perform the equality check through the Equals so VBCorLib can function correctly when handling objects.

The Equals method should be overridden to provide the type of equality checking that is appropriate for each class. Most classes simply check to see if the value is the same object instance as this object, meaning, they are both the same object in memory. For example CorDateTime checks for equality by comparing the dates of the two objects. And TimeSpan checks to see if the value has the same number of milliseconds to determine equality.

This method should not throw an exception. If a value passed in cannot be used for comparison then False should be returned.

Examples

The following code example demonstrates an implementation of the Equals method that may be more appropriate than the default method.

A Point class is created with the IObject interface appropriate for the class.

There are two modules created for this example. A BAS module containing the Main method and a Class module implementing the IComparer interface.

The following is a Visual Basic Class module named Point.

Option Explicit
Implements IObject

Public X As Long
Public Y As Long


Public Function Copy() As Point
    Set Copy = New Point
    Copy.X = X
    Copy.Y = Y
End Function

Public Function Equals(ByRef Value As Variant) As Boolean
    Dim Other As Point
    
    ' we check for equality by comparing the X and Y
    ' values from the other Point object.
    On Error GoTo Catch
    Set Other = Value
    Equals = Other.X = X And Other.Y = Y
    
Catch:
End Function

Public Function GetHashCode() As Long
    ' generate a hashcode using fields that are
    ' used to represent this Point object.
    GetHashCode = LShift(X, 1) Xor Y
End Function

Public Function ToString() As String
    ' output a string representation of this Point object.
    ToString = CorString.Format("({0}, {1})", X, Y)
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IObject Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function IObject_Equals(Value As Variant) As Boolean
    IObject_Equals = Equals(Value)
End Function

Private Function IObject_GetHashCode() As Long
    IObject_GetHashCode = GetHashCode
End Function

Private Function IObject_ToString() As String
    IObject_ToString = ToString
End Function

The following is a Visual Basic BAS module containing Main start-up method.

Public Sub Main()
    Dim P1 As New Point
    Dim P2 As Point
    
    P1.X = 1
    P1.Y = 2
    
    Set P2 = P1.Copy
    
    Console.WriteLine P1 Is P2
    Console.WriteLine Object.Equals(P1, P2)
    Console.WriteLine "P1's value is: {0}", P1
    Console.ReadKey
    
' This example produces the following output:
' False
' True
' P1's value is: (1, 2)
End Sub

See Also

Project CorLib Overview

Class IObject Overview