Re: Compare Two Structure Data Types...
From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 12/10/04
- Next message: Peter Proost: "Re: new icon for custom control"
- Previous message: GrandpaB: "Declaring & Initializing Classes & ArrayLists"
- In reply to: Kiran B.: "Compare Two Structure Data Types..."
- Messages sorted by: [ date ] [ thread ]
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.
>
- Next message: Peter Proost: "Re: new icon for custom control"
- Previous message: GrandpaB: "Declaring & Initializing Classes & ArrayLists"
- In reply to: Kiran B.: "Compare Two Structure Data Types..."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|