Prev | Current Page 582 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

For example, you now know that reference types (class instances) define equality as an
identity test by default, and value types (structure instances) use value equality as an equivalence
test. Reference types get their default implementation from Object.Equals(), whereas
value types get their default implementation from System.ValueType??™s override of Equals().
All structure types implicitly derive from System.ValueType.
You should implement your own override of Equals() for structures that you define. You
can compare the fields of your object more efficiently; since you know their types and what
they are at compile time. Let??™s update the ComplexNumber example from previous sections,
converting it to a structure and implementing a custom Equals() override:
Imports System
Public Structure ComplexNumber
Implements IComparable
Private ReadOnly real As Double
Private ReadOnly imaginary As Double
Public Sub New(ByVal real As Double, ByVal imaginary As Double)
Me.real = real
Me.imaginary = imaginary
End Sub
Public Overloads Overrides Function Equals(ByVal other As Object) As Boolean
Dim result As Boolean = False
If TypeOf other Is ComplexNumber Then
Dim that As ComplexNumber = CType(other, ComplexNumber)
result = InternalEquals(that)
End If
CHAPTER 14 n VB 2008 BEST PRACTICES 363
Return result
End Function
Public Overrides Function GetHashCode() As Integer
Return CInt(Fix(Me.


Pages:
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
śmieszne dowcipy dieta light życzenia ślubne portal Connie Talbot