By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

Compare Two Structure Data Types...

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Kiran B." wrote:
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.

Nov 21 '05 #2

P: n/a
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 B." <kb********@yahoo.com> wrote in message
news:Od**************@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.

Nov 21 '05 #3

P: n/a
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." <kb********@yahoo.com> wrote in message
news:Od**************@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.

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.