Create Complex Structure with Equals and GetHashCode 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 Complex Structure with Equals and GetHashCode method
 

Public Structure Complex
    Public re, im As Double

    Public Overrides Function Equals(ByVal obj As [Object]) As Boolean 
        Return TypeOf obj Is Complex AndAlso Me = CType(obj, Complex)
    End Function 

    Public Overrides Function GetHashCode() As Integer 
        Return re.GetHashCode() ^ im.GetHashCode()
    End Function 

    Public Shared Operator = (As Complex, y As ComplexAs Boolean
       Return x.re = y.re AndAlso x.im = y.im
    End Operator 

    Public Shared Operator <> (As Complex, y As ComplexAs Boolean
       Return Not (x = y)
    End Operator  
End Structure

Class Example
   Public Shared Sub Main() 
      Dim cmplx1, cmplx2 As Complex
      cmplx1.re = 4.0
      cmplx1.im = 1.0

      cmplx2.re = 2.0
      cmplx2.im = 1.0

      If cmplx1 <> cmplx2 Then
         Console.WriteLine("The two objects are not equal.")
      End If
      If Not cmplx1.Equals(cmplx2Then
         Console.WriteLine("The two objects are not equal.")
      End If

      cmplx2.re = 4.0

      If cmplx1.Equals(cmplx2Then
         Console.WriteLine("The two objects are now equal!")
      End If
      If cmplx1 = cmplx2 Then
         Console.WriteLine("The two objects are now equal!")
      End If
   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.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.