• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET Object.Equals方法代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.Object.Equals方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Object.Equals方法的具体用法?VB.NET Object.Equals怎么用?VB.NET Object.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。



在下文中一共展示了Object.Equals方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: Point

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

    Public Overrides Function Equals(obj As Object) As Boolean 
        ' Check for null and compare run-time types.
        If obj Is Nothing OrElse Not Me.GetType().Equals(obj.GetType()) Then
           Return False
        Else
           Dim p As Point = DirectCast(obj, Point)
           Return x = p.x AndAlso y = p.y
        End If
    End Function 

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

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

Class Point3D : Inherits Point
    Private z As Integer
    
    Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) 
        MyBase.New(x, y) 
        Me.z = Z
    End Sub

    Public Overrides Function Equals(ByVal obj As Object) As Boolean 
        Dim pt3 As Point3D = TryCast(obj, Point3D)
        If pt3 Is Nothing Then
           Return False
        Else
           Return MyBase.Equals(CType(pt3, Point)) AndAlso z = pt3.Z  
        End If
    End Function
    
    Public Overrides Function GetHashCode() As Integer 
        Return (MyBase.GetHashCode() << 2) XOr z
    End Function 
    
    Public Overrides Function ToString() As String
        Return String.Format("Point({0}, {1}, {2})", x, y, z)
    End Function
End Class 

Module Example
    Public Sub Main() 
        Dim point2D As New Point(5, 5)
        Dim point3Da As New Point3D(5, 5, 2)
        Dim point3Db As New Point3D(5, 5, 2)
        Dim point3Dc As New Point3D(5, 5, -1)
        
        Console.WriteLine("{0} = {1}: {2}", 
                          point2D, point3Da, point2D.Equals(point3Da))
        Console.WriteLine("{0} = {1}: {2}", 
                          point2D, point3Db, point2D.Equals(point3Db))        
        Console.WriteLine("{0} = {1}: {2}", 
                          point3Da, point3Db, point3Da.Equals(point3Db))
        Console.WriteLine("{0} = {1}: {2}", 
                          point3Da, point3Dc, point3Da.Equals(point3Dc))
    End Sub  
End Module 
' The example displays the following output
'       Point(5, 5) = Point(5, 5, 2): False
'       Point(5, 5) = Point(5, 5, 2): False
'       Point(5, 5, 2) = Point(5, 5, 2): True
'       Point(5, 5, 2) = Point(5, 5, -1): False
开发者ID:VB.NET开发者,项目名称:System,代码行数:80,代码来源:Object.Equals


示例2: Point

Class Rectangle 
    Private a, b As Point
    
    Public Sub New(ByVal upLeftX As Integer, ByVal upLeftY As Integer, _
                   ByVal downRightX As Integer, ByVal downRightY As Integer) 
        Me.a = New Point(upLeftX, upLeftY)
        Me.b = New Point(downRightX, downRightY)
    End Sub 
    
    Public Overrides Function Equals(ByVal obj As [Object]) As Boolean 
        ' Performs an equality check on two rectangles (Point object pairs).
        If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then
            Return False
        End If
        Dim r As Rectangle = CType(obj, Rectangle)
        Return a.Equals(r.a) AndAlso b.Equals(r.b)
    End Function

    Public Overrides Function GetHashCode() As Integer 
        Return Tuple.Create(a, b).GetHashCode()
    End Function 

    Public Overrides Function ToString() As String
       Return String.Format("Rectangle({0}, {1}, {2}, {3})",
                            a.x, a.y, b.x, b.y) 
    End Function
End Class 

Class Point
    Friend x As Integer
    Friend 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 
        ' Performs an equality check on two points (integer pairs).
        If obj Is Nothing OrElse Not [GetType]().Equals(obj.GetType()) Then
            Return False
        Else
           Dim p As Point = CType(obj, Point)
           Return x = p.x AndAlso y = p.y
        End If
    End Function 
    
    Public Overrides Function GetHashCode() As Integer 
        Return Tuple.Create(x, y).GetHashCode()
    End Function 
End Class  

Class Example
    Public Shared Sub Main() 
        Dim r1 As New Rectangle(0, 0, 100, 200)
        Dim r2 As New Rectangle(0, 0, 100, 200)
        Dim r3 As New Rectangle(0, 0, 150, 200)
        
        Console.WriteLine("{0} = {1}: {2}", r1, r2, r1.Equals(r2))
        Console.WriteLine("{0} = {1}: {2}", r1, r3, r1.Equals(r3))
        Console.WriteLine("{0} = {1}: {2}", r2, r3, r2.Equals(r3))
    End Sub 
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:63,代码来源:Object.Equals

输出:

Rectangle(0, 0, 100, 200) = Rectangle(0, 0, 100, 200): True
Rectangle(0, 0, 100, 200) = Rectangle(0, 0, 150, 200): False
Rectangle(0, 0, 100, 200) = Rectangle(0, 0, 150, 200): False


示例3: Example

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 Tuple.Create(re, im).GetHashCode()
    End Function 
    
    Public Shared Operator = (x As Complex, y As Complex) As Boolean
       Return x.re = y.re AndAlso x.im = y.im
    End Operator 
    
    Public Shared Operator <> (x As Complex, y As Complex) As Boolean
       Return Not (x = y)
    End Operator 
    
    Public Overrides Function ToString() As String
       Return String.Format("({0}, {1})", re, im)
    End Function 
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

      Console.WriteLine("{0} <> {1}: {2}", cmplx1, cmplx2, cmplx1 <> cmplx2)        
      Console.WriteLine("{0} = {1}: {2}", cmplx1, cmplx2, cmplx1.Equals(cmplx2))        
      
      cmplx2.re = 4.0
        
      Console.WriteLine("{0} = {1}: {2}", cmplx1, cmplx2, cmplx1 = cmplx2)        
      Console.WriteLine("{0} = {1}: {2}", cmplx1, cmplx2, cmplx1.Equals(cmplx2))        
   End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:43,代码来源:Object.Equals

输出:

(4, 1) <> (2, 1): True
(4, 1) = (2, 1): False
(4, 1) = (4, 1): True
(4, 1) = (4, 1): True


示例4: Person

' Define a reference type that does not override Equals.
Public Class Person
   Private personName As String
   
   Public Sub New(name As String)
      Me.personName = name
   End Sub
   
   Public Overrides Function ToString() As String
      Return Me.personName
   End Function 
End Class

Module Example
   Public Sub Main()
      Dim person1a As New Person("John")
      Dim person1b As Person = person1a
      Dim person2 As New Person(person1a.ToString())
      
      Console.WriteLine("Calling Equals:") 
      Console.WriteLine("person1a and person1b: {0}", person1a.Equals(person1b))               
      Console.WriteLine("person1a and person2: {0}", person1a.Equals(person2))  
      Console.WriteLine()
      
      Console.WriteLine("Casting to an Object and calling Equals:")
      Console.WriteLine("person1a and person1b: {0}", CObj(person1a).Equals(CObj(person1b)))
      Console.WriteLine("person1a and person2: {0}", CObj(person1a).Equals(CObj(person2))) 
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:29,代码来源:Object.Equals

输出:

Calling Equals:
person1a and person1b: True
person1a and person2: False

Casting to an Object and calling Equals:
person1a and person1b: True
person1a and person2: False


示例5: Example

Module Example
   Public Sub Main()
      Dim value1 As Byte = 12
      Dim value2 As Integer = 12
      
      Dim object1 As Object = value1
      Dim object2 As Object = value2
      
      Console.WriteLine("{0} ({1}) = {2} ({3}): {4}",
                        object1, object1.GetType().Name,
                        object2, object2.GetType().Name,
                        object1.Equals(object2))
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:14,代码来源:Object.Equals

输出:

12 (Byte) = 12 (Int32): False


示例6: Example

' Define a value type that does not override Equals.
Public Structure Person
   Private personName As String
   
   Public Sub New(name As String)
      Me.personName = name
   End Sub
   
   Public Overrides Function ToString() As String
      Return Me.personName
   End Function 
End Structure

Module Example
   Public Sub Main()
      Dim p1 As New Person("John")
      Dim p2 As New Person("John")
      
      Console.WriteLine("Calling Equals:") 
      Console.WriteLine(p1.Equals(p2))
      Console.WriteLine()
      
      Console.WriteLine("Casting to an Object and calling Equals:")
      Console.WriteLine(CObj(p1).Equals(p2))
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:26,代码来源:Object.Equals

输出:

Calling Equals:
True

Casting to an Object and calling Equals:
True


示例7: Example

' 导入命名空间
Imports System.Text

Module Example
   Public Sub Main()
      Dim sb1 As New StringBuilder("building a string...")
      Dim sb2 As New StringBuilder("building a string...")
      
      Console.WriteLine("sb1.Equals(sb2): {0}", sb1.Equals(sb2))
      Console.WriteLine("CObj(sb1).Equals(sb2): {0}", 
                        CObj(sb1).Equals(sb2))
      Console.WriteLine("Object.Equals(sb1, sb2): {0}",
                        Object.Equals(sb1, sb2))                  
      
      Console.WriteLine()
      Dim sb3 As Object = New StringBuilder("building a string...")
      Console.WriteLine("sb3.Equals(sb2): {0}", sb3.Equals(sb2))                              
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:19,代码来源:Object.Equals

输出:

sb1.Equals(sb2): True
CObj(sb1).Equals(sb2): False
Object.Equals(sb1, sb2): False

sb3.Equals(sb2): False


示例8: Person

Public Class Person
   Private idNumber As String
   Private personName As String
   
   Public Sub New(name As String, id As String)
      Me.personName = name
      Me.idNumber = id
   End Sub
   
   Public Overrides Function Equals(obj As Object) As Boolean
      Dim personObj As Person = TryCast(obj, Person) 
      If personObj Is Nothing Then
         Return False
      Else
         Return idNumber.Equals(personObj.idNumber)
      End If   
   End Function
   
   Public Overrides Function GetHashCode() As Integer
      Return Me.idNumber.GetHashCode() 
   End Function
End Class

Module Example
   Public Sub Main()
      Dim p1 As New Person("John", "63412895")
      Dim p2 As New Person("Jack", "63412895")
      Console.WriteLine(p1.Equals(p2))
      Console.WriteLine(Object.Equals(p1, p2))
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:31,代码来源:Object.Equals

输出:

True
True


示例9: Example

Module Example
   Public Sub Main()
      Dim m1 As New Dog("Alaskan Malamute")
      Dim m2 As New Dog("Alaskan Malamute")
      Dim g1 As New Dog("Great Pyrenees")
      Dim g2 As Dog = g1
      Dim d1 As New Dog("Dalmation")
      Dim n1 As Dog = Nothing
      Dim n2 As Dog = Nothing
      
      Console.WriteLine("null = null: {0}", Object.Equals(n1, n2))
      Console.WriteLine("null Reference Equals null: {0}", Object.ReferenceEquals(n1, n2))
      Console.WriteLine()
      
      Console.WriteLine("{0} = {1}: {2}", g1, g2, Object.Equals(g1, g2))
      Console.WriteLine("{0} Reference Equals {1}: {2}", g1, g2, Object.ReferenceEquals(g1, g2))
      Console.WriteLine()
      
      Console.WriteLine("{0} = {1}: {2}", m1, m2, Object.Equals(m1, m2))
      Console.WriteLine("{0} Reference Equals {1}: {2}", m1, m2, Object.ReferenceEquals(m1, m2))
      Console.WriteLine()
      
      Console.WriteLine("{0} = {1}: {2}", m1, d1, Object.Equals(m1, d1))  
      Console.WriteLine("{0} Reference Equals {1}: {2}", m1, d1, Object.ReferenceEquals(m1, d1))  
   End Sub
End Module

Public Class Dog
   ' Public field.
   Public Breed As String
   
   ' Class constructor.
   Public Sub New(dogBreed As String)
      Me.Breed = dogBreed
   End Sub

   Public Overrides Function Equals(obj As Object) As Boolean
      If obj Is Nothing OrElse Not typeof obj Is Dog Then
         Return False
      Else
         Return Me.Breed = CType(obj, Dog).Breed
      End If   
   End Function
   
   Public Overrides Function GetHashCode() As Integer
      Return Me.Breed.GetHashCode()
   End Function
   
   Public Overrides Function ToString() As String
      Return Me.Breed
   End Function
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:52,代码来源:Object.Equals

输出:

null = null: True
null Reference Equals null: True

Great Pyrenees = Great Pyrenees: True
Great Pyrenees Reference Equals Great Pyrenees: True

Alaskan Malamute = Alaskan Malamute: True
Alaskan Malamute Reference Equals Alaskan Malamute: False

Alaskan Malamute = Dalmation: False
Alaskan Malamute Reference Equals Dalmation: False



注:本文中的System.Object.Equals方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET Random.Sample方法代码示例发布时间:2022-05-24
下一篇:
VB.NET Console.Out属性代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap