Another valuable method of the Object class is called
Equals. This method is used to compare two instances of a class for
equality. This method is overloaded with two versions and each returns a Boolean
value.
One of the versions of the Object.Equals()
method has the following syntax:
Overloads Public Overridable Function Equals(ByVal obj As Object) As Boolean
This method can be called by any class of a .NET
Framework application and it takes as argument an instance of the class
that the called needs to be compared to. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim number1 As Integer, number2 As Integer
number1 = 248
number2 = 2480
Response.Write("{0} = {1}: {2}", number1, number2, _
number1.Equals(number2))
Return 0
End Function
End Module
This would produce:
248 = 2480: False
The second version of the Object.Equals()
method has the following syntax:
Overloads Public Shared Function Equals(ByVal objA As Object,_ ByVal objB As Object) As Boolean
This version is declared as Shared. This means
that it is not called by a specific instance of a class. Instead, it takes
two arguments that each represents an instance of the classes that need to
be compared. Here is an example of calling it:
Module Exercise
Public Function Main() As Integer
Dim Country As String, Pais As String
Country = "Senegal"
Pais = "Senegal"
Response.Write("{0} = {1}: {2}", Country, Pais, _
Equals(Country, Pais))
Return 0
End Function
End Module
This would produce:
Senegal = Senegal: True
Although this method is made available to all .NET
classes by through inheritance from the Object class, in most
cases, to make sure it rightly behaves, you should customize its
implementation in most of your classes where you intend to call it.
Consider the following program:
Module Exercise
Public Class Square
Inherits Object
Public Side As Double
Function CalculatePerimeter() As Double
Return Side * 4
End Function
Function CalculateArea() As Double
Return Side * Side
End Function
Public Overrides Function ToString() As String
Return "Square Characteristics" & vbCrLf & _
"Side: " & CStr(Side) & vbCrLf & _
"Perimeter: " & CStr(CalculatePerimeter()) & vbCrLf & _
"Area: " & CStr(CalculateArea())
End Function
End Class
Public Function Main() As Integer
Dim sqr1 As Square = New Square
Dim sqr2 As Square = New Square
Response.Write(" =+= First Square =+=")
Console.Write("Enter Side: ")
sqr1.Side = Double.Parse(Console.ReadLine())
Response.Write(" =+= Second Square =+=")
Console.Write("Enter Side: ")
sqr2.Side = Double.Parse(Console.ReadLine())
Response.Write()
Response.Write("" & sqr1.ToString())
Response.Write()
Response.Write("" & sqr2.ToString())
Response.Write()
Response.Write("Squares Equality: " & sqr1.Equals(sqr2))
Return 0
End Function
End Module
Here is an example of executing it:
=+= First Square =+=
Enter Side: 125.84
=+= Second Square =+=
Enter Side: 125.84
Square Characteristics
Side: 125.84
Perimeter: 503.36
Area: 15835.71
Square Characteristics
Side: 125.84
Perimeter: 503.36
Area: 15835.71
Squares Equality: False
Notice that, although both square instances have the
same Side value and produce the same area, the compiler renders them not
equal. This is an indication that the compiler doesn't know how to compare
two instances of the Square class. The solution to this type of problem is
to override the Equals() method in your class instead of relying on
the default implementation from the Object class. Here are two
overrides of the Equals() methods as overridden for the above Square
class:
Module Exercise Public Class Square Inherits Object Public Side As Double Function CalculatePerimeter() As Double Return Side * 4 End Function Function CalculateArea() As Double Return Side * Side End Function Public Overrides Function ToString() As String Return "Square Characteristics" & vbCrLf & _ "Side: " & CStr(Side) & vbCrLf & _ "Perimeter: " & CStr(CalculatePerimeter()) & vbCrLf & _ "Area: " & CStr(CalculateArea()) End Function Public Overridable Overloads Function Equals(ByVal sqr As Square) As Boolean ' We will only compare the side of the square ' because the calculations of the perimeter and the area ' directly depend on the side ' If the side of the square passed as argument is equal ' to the side of this object, both objects are equal If sqr.Side = Me.Side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Public Overloads Shared Function Equals(ByVal first As Square, _ ByVal second As Square) As Boolean ' We will only compare the side of the square ' If the side of the first square is equal ' to the side of the second one, then both squares are equal If first.Side = second.Side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function End Class Public Function Main() As Integer Dim sqr1 As Square = New Square Dim sqr2 As Square = New Square Response.Write(" =+= First Square =+=") Console.Write("Enter Side: ") sqr1.Side = Double.Parse(Console.ReadLine()) Response.Write(" =+= Second Square =+=") Console.Write("Enter Side: ") sqr2.Side = Double.Parse(Console.ReadLine()) Response.Write() Response.Write("" & sqr1.ToString()) Response.Write("" & sqr2.ToString()) Response.Write("Squares Equality: " & sqr1.Equals(sqr2)) Response.Write() Response.Write(" =+= First Square =+=") Console.Write("Enter Side: ") sqr1.Side = Double.Parse(Console.ReadLine()) Response.Write(" =+= Second Square =+=") Console.Write("Enter Side: ") sqr2.Side = Double.Parse(Console.ReadLine()) Response.Write() Response.Write("" & sqr1.ToString()) Response.Write() Response.Write("" & sqr2.ToString()) Response.Write() Response.Write("Squares Equality: " & Equals(sqr1, sqr2)) Return 0 End Function End Module
Here is an example of testing the program:
=+= First Square =+= Enter Side: 125.84 =+= Second Square =+= Enter Side: 125.84 Square Characteristics Side: 125.84 Perimeter: 503.36 Area: 15835.71 Square Characteristics Side: 125.84 Perimeter: 503.36 Area: 15835.71 Squares Equality: True =+= First Square =+= Enter Side: 38.45 =+= Second Square =+= Enter Side: 16.82 Square Characteristics Side: 38.45 Perimeter: 153.80 Area: 1478.40 Square Characteristics Side: 16.82 Perimeter: 67.28 Area: 282.91 Squares Equality: False
Here is another run of the same program:
=+= First Square =+= Enter Side: 70.68 =+= Second Square =+= Enter Side: 42.04 Square Characteristics Side: 70.68 Perimeter: 282.72 Area: 4995.66 Square Characteristics Side: 42.04 Perimeter: 168.16 Area: 1767.36 Squares Equality: False =+= First Square =+= Enter Side: 58.26 =+= Second Square =+= Enter Side: 58.26 Square Characteristics Side: 58.26 Perimeter: 233.04 Area: 3394.23 Square Characteristics Side: 58.26 Perimeter: 233.04 Area: 3394.23 Squares Equality: True
Notice that, this time, the compiler knows how to
perform the comparison of two Square objects using either version of the Equals()
method.
|
Built-In Classes: Object
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment