Create your own Equals method : Object « Development « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » Development » Object 




Create your own Equals method
 
Imports System

Class Point
    Inherits Object
    Protected x, y As Integer
    Public Sub New() 
        Me.x = 0
        Me.y = 0
    End Sub 'New

    Public Sub New(ByVal X As Integer, ByVal Y As Integer
        Me.x = X
        Me.y = Y
    End Sub 'New

    Public Overrides Function Equals(ByVal obj As ObjectAs Boolean 
        If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then
            Return False
        End If
        Dim As Point = CType(obj, Point)
        Return x = p.x AndAlso y = p.y
    End Function 'Equals

    Public Overrides Function GetHashCode() As Integer 
        Return x ^ y
    End Function 'GetHashCode
End Class 'Point
Class Point3D
    Inherits Point
    Private As Integer

    Public Sub New(ByVal X As Integer, ByVal Y As Integer, ByVal Z As Integer
        Me.x = X
        Me.y = Y
        Me.z = Z
    End Sub 'New

    Public Overrides Function Equals(ByVal obj As ObjectAs Boolean 
        Return MyBase.Equals(objAndAlso z = CType(obj, Point3D).z
    End Function 'Equals

    Public Overrides Function GetHashCode() As Integer 
        Return MyBase.GetHashCode() ^ z
    End Function 'GetHashCode
End Class 'Point3D

Class [MyClass]
    Public Shared Sub Main() 
        Dim point2D As New Point(55)
        Dim point3Da As New Point3D(552)
        Dim point3Db As New Point3D(552)

        If Not point2D.Equals(point3DaThen
            Console.WriteLine("point2D does not equal point3Da.")
        End If
        If Not point3Db.Equals(point2DThen
            Console.WriteLine("Likewise, point3Db does not equal point2D.")
        End If
        If point3Da.Equals(point3DbThen
            Console.WriteLine("However, point3Da equals point3Db.")
        End If

    End Sub 'Main 
End Class '[MyClass]

   
  














Related examples in the same category
1.Object Class Supports all classes in the .NET Framework class hierarchy
2.Object.Equals Method tells whether the specified Object is equal to the current Object.
3.Create Equals method based on Equals method from fields
4.Create Complex Structure with Equals and GetHashCode method
5.Object.GetType Method Gets the Type of the current instance.
6.Object.MemberwiseClone Method Creates a shallow copy of the current Object.
7.Object Class Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes.
8.Object.GetType Method Gets the Type of the current instance.
9.Object.MemberwiseClone Method Creates a shallow copy of the current Object.
10.Object Class Supports all classes in the .NET Framework class hierarchy
11.Object.GetType Method Gets the Type of the current instance.
12.Object.MemberwiseClone Creates a shallow copy of the current Object.
13.No implementation. All members are inherited from Object
14.Overrides ToString function
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.