Compare Two Structure Data Types...

  • Thread starter Thread starter Kiran B.
  • Start date Start date
K

Kiran B.

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.
 
You cannot make a direct comparison between two different types, even if they
are otherwise identical. Since your two structures are set up identically,
you could use one structure instead:

Dim struct1 as New A
dim struct2 as New A

dim n1 as String = "Bill"
din n2 as String = "bill"

struct1.Fname = n1.ToLower
struct2.Fname = n2.ToLower

If struct1.Equal(struct2) then
messagebox.show("equal")
end if

Since the Structure is not a simple datatype, you must use equals, rather
than " = ", which you would use for simple data types.

For example, if you had two separate structures, you could compare any
string value in one structure with any string value in another structure
using " = ".

www.charlesfarriersoftware.com
 
You have to compare each field by itself. Either write a function that
accept two Structures and then checks and return true or false, or convert
them to classes and make a method that accept the other item and compares
them.

Only way I know how.

Chris
 
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. said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top