472,353 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Compare Two Structure Data Types...

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
3 8600
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class...
4
by: troloo | last post by:
Hello, I hope you can help me :)) The story goes as follows: I have a class with different methods and member variables. I store pointers to...
4
by: No One | last post by:
Here is my problem: I have a certain set of well-defined manipulations that I have to apply to different types of data. In all cases the...
5
by: Craig | last post by:
Hi everyone, As a relative new-comer to the wonderful world of .NET Framework and the C# langauge I have come across something that I would like...
3
by: person | last post by:
Hi, Is there an STL function/algorithm to compare two maps and return a map of the differences? I'm looking for a way to do an "exclusive or"...
4
by: Abdul Samad | last post by:
banfa just said that it is because so the compiler may not assign the same memory to two variables but my question is WHY DOES COPILER ASSIGNMS...
1
by: WELCOME ### | last post by:
From Beginner : With Visual C++, there're 3 ways to create Windows Applications. One of these is by using Windows API (Application Program...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this ...
1
by: bantunks | last post by:
Hello, I am trying to figure out the advantages and disadvantages of exposing interfaces through Opaque data types in C. I have figured/found out...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.