---------------------------------------------------------------------------------------

View Previous Wolf Tracks Columns

Submit a Question to Wolf Tracks


Testing for Equality
Q from Boopathi:

What is the equivalent to IsEqualTo in Visual Basic .NET?
--------------------------------------------------------------------------------
Howling Wolf Consulting Services

Wolf Tracks

A Q&A Forum

Typically, the IsEqualTo function is used in some languages, such as Java, to test for an
equality of values. For example, if the function has the syntax

Public Function IsEqualTo(obj1 As Object, obj2 As Object) As Boolean

then we could use the function in a pseudo-code as follows:

Dim firstNumber As Integer = 102
Dim secondNumber As Integer = 102

Dim flagEquality As Boolean

flagEquality = IsEqualTo(firstNumber, SecondNumber)   ' Returns True

In Visual Basic .NET (as well as in the pre-.NET versions of Visual Basic), the equals sign,
when it is used as a comparison operator rather than an assignment operator, performs the same
function as
IsEqualTo. For example, we could rewrite the previous code fragment in legal
Visual Basic code as follows:

Dim firstNumber As Integer = 102
Dim secondNumber As Integer = 102
Dim flagEquality As Boolean

flagEquality = (firstNumber = secondNumber)
Console.WriteLine(flagEquality)                       ' Returns True

The .NET Framework also provides the functionality to test for equal values. For .NET value
types, this is done by calling the type's
Equals method. For example:

Dim firstNumber As Integer = 102
Dim secondNumber As Integer = 102
Dim flagEquality As Boolean

flagEquality = firstNumber.Equals(secondNumber)
Console.WriteLine(flagEquality)                       ' Returns True

A word of caution is in order here, though. Whether or not Equals tests for an equality of
values depends on the type of the instance whose
Equals method is called. For value types
(like Booleans, Integers, Longs, and Singles) and for Strings (which are a reference type),
Equals tests for an equality of values. For non-String reference types (that is, for instances of
classes and for objects that do not hold a value type), the
Equals method generally tests for
an equality of references, which (as we'll see in a moment) is a very different thing. For instance,
consider the following:

Public Class Equality
Public Shared Sub Main()
 Dim onePerson As New Person("John")
 Dim secondPerson As New Person("John")
 Dim flagEquality As Boolean

 flagEquality = onePerson.Equals(secondPerson)
 Console.WriteLine(flagEquality)                    ' Returns False
End Sub
End Class

Public Class Person
Private personName As String

Public Sub New(n As String)
 personName = n
End Sub

Public ReadOnly Property Name() As String
 Get
    Return personName
 End Get
End Property
End Class

Here, we've created two instances of the Person class and assigned them the same name. The
objects, then, have equal values. However, the test for equality returns False. This is because
class instances are reference types, and ordinarily the
Equals method, when called from a
non-string reference type, tests to see whether two object variables reference the same object
instance. ("Ordinarily" is an important qualifier here, since the
Equals method can be
overridden in derived classes. In a custom class, such as
Person, we could define it to test for
the equality of one or more property values, for instance.)

If we modify the previous code so that
secondPerson is set equal to firstPerson,
then the call to the
Equals method returns True. This is because we have two object
variables, but both of them reference a single
Person object instance. Here is the code:

Public Shared Sub Main()
Dim onePerson As New Person("John")
Dim secondPerson As Person = onePerson
Dim flagEquality As Boolean

flagEquality = onePerson.Equals(secondPerson)
Console.WriteLine(flagEquality)                       ' Returns True
End Sub

If this conditional meaning of the Equals method is confusing, you can instead call the
ReferenceEquals method for reference types, which avoids the possible confusion
between a test for equal values and a test for equivalent references.
ReferenceEquals is a
member of the .NET
Object class and is inherited by all reference types derived from
Object. We could use it in code as follows:

Dim onePerson As New Person("John")
Dim secondPerson As Person = onePerson
Dim flagEquality As Boolean

flagEquality = Person.ReferenceEquals(onePerson, secondPerson)
Console.WriteLine(flagEquality)                       ' Returns True

You can also test for equal object references by using the Visual Basic .NET Is operator,
which is equivalent to a call to the
ReferenceEquals method or to the Equals method
for most non-string reference types. For example:

Dim onePerson As New Person("John")
Dim secondPerson As Person = onePerson
Dim flagEquality As Boolean

flagEquality = (onePerson Is secondPerson)
Console.WriteLine(flagEquality)                       ' Returns True

The Is operator can also be used with the special Nothing keyword to test for a null object
reference. For example:

Dim secondPerson As Person = onePerson
Dim flagEquality As Boolean

flagEquality = (onePerson Is secondPerson)
Console.WriteLine(flagEquality)                       ' Returns True
If Not onePerson Is Nothing Then
onePerson = Nothing
End If
View Previous Columns

Submit a Question