Re: Compare Two Structure Data Types...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 12/10/04


Date: Fri, 10 Dec 2004 08:45:52 -0600

Kiran,
> Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and
> Structure B.
Why two identical structure types, why not a single type with two instances?

Encapsulation, one of the tenants of OO, says that the Structure itself
should know how to compare itself to others of its type.

I normally override & overload the Equals method on the Structure to do the
comparison. With VS.NET 2005 (aka Whidbey, due out later in 2005) you will
be able to overload the equals "=" operator as well. In the case of
Structure A = Structure B, I would overload Equals for the two types...

    Dim theA As A
    Dim theB As A

> If theA.Equals(theB) Then
>
> Do tthis ..
> Else
>
> End if

    Structure A
        Public Fname As String
        Public LastName As String
        Public City As String
        Public Zip As String

        Public Overloads Function Equals(ByVal other As A) As Boolean
            Return Fname = other.Fname _
                AndAlso LastName = other.LastName _
                AndAlso City = other.City _
                AndAlso Zip = other.Zip
        End Function

        Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
            If TypeOf obj Is A Then
                Return Equals(DirectCast(obj, A))
            Else
                Return False
            End If
        End Function

    End Structure

You could overload the Equals function a third time if you want to compare
to Structure B also...

        Public Overloads Function Equals(ByVal other As B) As Boolean
            Return Fname = other.Fname _
                AndAlso LastName = other.LastName _
                AndAlso City = other.City _
                AndAlso Zip = other.Zip
        End Function

    Dim theA As A
    Dim theB As B

    If theA.Equals(theB) Then

If I overloaded Equals in A for B, I would also overload Equals in B for A,
so the operation was symmetrical.

    Dim theA As A
    Dim theB As B

    Debug.Assert(theA.Equals(theB) = theB.Equals(theA), "Symmetrical Equals
A=B!")

Also if you override Equals, you should consider overriding GetHashCode as
well.

        Public Overrides Function GetHashCode() As Integer
            Return Fname.GetHashCode() _
                Xor LastName.GetHashCode() _
                Xor City.GetHashCode() _
                Xor Zip.GetHashCode()
        End Function

Overriding GetHashCode allows your structure to be used as a key in a
HashTable, if you use your Structure as a Key, you should make all the
fields immutable (ReadOnly).

Hope this helps
Jay

"Kiran B." <kbyajankar@yahoo.com> wrote in message
news:Odn40jl3EHA.2608@TK2MSFTNGP10.phx.gbl...
> Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and
> Structure B.
>
> Structure A
> Public Fname as String
> Public LastName as String
> Public City as String
> Public Zip as String
> End Structure
>
>
> Structrure B
> Public Fname as String
> Public LastName as String
> Public City as String
> Public Zip as String
> End Structure
>
> How can I compare these two data types.. ie Fname, LastName, City, Zip of
> Structure A should match Fname, LastName, City, Zip of Sturcture
> [shouldn't be ase sensitive.]
>
> If Sturcture A = Structure Then
>
> Do tthis ..
> Else
>
> End if
>
>
> Thanks in advance.
>



Relevant Pages

  • Re: Function that returns date of file.
    ... string after the date/time when it is used by itself. ... Is that your entire script? ... I make an IF statement that required the 'equals equals'. ... designed database your job will be all that much harder. ...
    (alt.php)
  • Re: Overriding Equals method not being called when doing IndexOf an item in an ArrayList
    ... | I have a class that I can compare to a string, ... I normally overload Equals to have both Equals& ... |> Tom, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: == versus .Equals()
    ... whether you use the == operator or the Equals method. ... To explain the differences I will use the String class as an example. ... the String class uses the static Equals method ... a string compare. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Catching invalid string comparisons
    ... the programmer expected it to check if it is equal to an empty string. ... compares for equality -- but two references are equal ... Comparing the objects themselves, ... requires use of the equals() method. ...
    (comp.lang.java.programmer)
  • Re: hashCode() for Custom classes
    ... I am currently faced with the task of providing a logical equals() ... have to override the hashCode() so that when an object of this class ... String name; ... The hash code of Agent is the combined hash code of ...
    (comp.lang.java.programmer)