Object Class Supports all classes in the .NET Framework class hierarchy : Object « Development « VB.Net






Object Class Supports all classes in the .NET Framework class hierarchy

  

Class Point
    Public x, y As Integer
    Public Sub New(ByVal x As Integer, ByVal y As Integer) 
        Me.x = x
        Me.y = y
    End Sub

    Public Overrides Function Equals(ByVal obj As Object) As Boolean 
        Dim objType As Type = obj.GetType()
        Dim meType  As Type = Me.GetType()
        If Not objType.Equals(meType) Then
            Return False
        End If 
        Dim other As Point = CType(obj, Point)
        Return Me.x = other.x AndAlso Me.y = other.y
    End Function 

    Public Overrides Function GetHashCode() As Integer 
        Return x XOr y
    End Function 

    Public Overrides Function ToString() As String 
        Return String.Format("({0}, {1})", x, y)
    End Function

    Public Function Copy() As Point 
        Return CType(Me.MemberwiseClone(), Point)
    End Function
End Class  

Public Class App
    Shared Sub Main() 

        Dim p1 As New Point(1, 2)
        Dim p2 As Point = p1.Copy()
        Dim p3 As Point = p1

        Console.WriteLine([Object].ReferenceEquals(p1, p2))
        Console.WriteLine([Object].Equals(p1, p2))
        Console.WriteLine([Object].ReferenceEquals(p1, p3))
        Console.WriteLine("p1's value is: {0}", p1.ToString())

    End Sub
End Class

   
    
  








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 your own Equals method
4.Create Equals method based on Equals method from fields
5.Create Complex Structure with Equals and GetHashCode method
6.Object.GetType Method Gets the Type of the current instance.
7.Object.MemberwiseClone Method Creates a shallow copy of the current Object.
8.Object Class Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes.
9.Object.GetType Method Gets the Type of the current instance.
10.Object.MemberwiseClone Method Creates a shallow copy of the current Object.
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