473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

comparing fields in 2 different instances of the same object

I have a class called products. In my maintenance application I am instantiating two versions of this class.

mProduct is used in all transactions and changes
mProduct_original is loaded at the same time as the original mProduct is instantiated.

I would like to know if there is a simple way to iterate through the properties of each and compare them for changes?

--
--Eric Cathell, MCSA
Nov 21 '05 #1
8 1504
ok I have figured out so far I can do this with reflection. here is what I have so far.

Private Sub checkForChanges(ByVal pType As Type) ', ByVal poType As Type)

Dim productProperties() As PropertyInfo = pType.GetProperties(BindingFlags.Public Or BindingFlags.Instance)

Dim PropertyItem As PropertyInfo

For Each PropertyItem In productProperties

With PropertyItem

Debug.WriteLine(PropertyItem.Name)

debug.WriteLine(mproduct.property



End With



Next





End Sub

I got this far with an article from O'reilly. Now how do I use the propertyitem.name to tie in with the actual class property? In otherwords, If propertyname is Customername who do I use that to access product.customername's value so that I can compare it to product_original.customername?


--
--Eric Cathell, MCSA
"ECathell" <ec******@nospam.mountaire.com> wrote in message news:uZ****************@tk2msftngp13.phx.gbl...
I have a class called products. In my maintenance application I am instantiating two versions of this class.

mProduct is used in all transactions and changes
mProduct_original is loaded at the same time as the original mProduct is instantiated.

I would like to know if there is a simple way to iterate through the properties of each and compare them for changes?

--
--Eric Cathell, MCSA
Nov 21 '05 #2
IMHO, the best thing to do would be to have you class implement the
IComparable interface and then write the code to compare the two objects
and return the results.

Hope this helps,
Brian Swanson

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
I have a class called products. In my maintenance application I am
instantiating two versions of this class.

mProduct is used in all transactions and changes
mProduct_original is loaded at the same time as the original mProduct is
instantiated.

I would like to know if there is a simple way to iterate through the
properties of each and compare them for changes?

--
--Eric Cathell, MCSA


Nov 21 '05 #3
everything I find on icomparable shows it only as a method for sorting data.
How would I use it to compare the differences in the properties of the
Product object?
--
--Eric Cathell, MCSA
"Brian Swanson" <pu*******@gREMOVETHISmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
IMHO, the best thing to do would be to have you class implement the
IComparable interface and then write the code to compare the two objects
and return the results.

Hope this helps,
Brian Swanson

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
I have a class called products. In my maintenance application I am
instantiating two versions of this class.

mProduct is used in all transactions and changes
mProduct_original is loaded at the same time as the original mProduct is
instantiated.

I would like to know if there is a simple way to iterate through the
properties of each and compare them for changes?

--
--Eric Cathell, MCSA

Nov 21 '05 #4
Having a class implement IComparable gives it a "CompareTo" method which
can takes an object as a parameter, you then compare the current
instance of your class (that was used to run CompareTo) to the instance
that was passed as a parameter.

I'll post an example if you need it, just let me know...

Hope this helps,
Brian Swanson

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
everything I find on icomparable shows it only as a method for sorting
data.
How would I use it to compare the differences in the properties of the
Product object?
--
--Eric Cathell, MCSA
"Brian Swanson" <pu*******@gREMOVETHISmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
IMHO, the best thing to do would be to have you class implement the
IComparable interface and then write the code to compare the two objects

and return the results.

Hope this helps,
Brian Swanson

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
I have a class called products. In my maintenance application I am
instantiating two versions of this class.

mProduct is used in all transactions and changes
mProduct_original is loaded at the same time as the original mProduct
is
instantiated.

I would like to know if there is a simple way to iterate through the
properties of each and compare them for changes?

--
--Eric Cathell, MCSA


Nov 21 '05 #5
hmmm, I dont think that will still work because I am trying to compare the Properties of the class to see if they have been changed... here is some more code hope it helps...

mProduct = New Mountaire.Database.Product(txtPLUnumber.Text, mCurrentFacility.Location)

mProduct_Original = New Mountaire.Database.Product(txtPLUnumber.Text, mCurrentFacility.Location)



populateProductFields(mProduct)

This first bit creates 2 new product objects. The populateproductfields populates text fields and boxes with the information on that product.

When the user moves off this particular product I need to verify that no changes were made. The easiest way for me to do this is to compare the properties of both product objects to see if they are different.

there are 45 or so properties of the product class that need to be compared. If I understand you right if I compare the 2 alone it will still work?

ie

private function CompareTo(by val obj as object) as integer implements icomparable.compareto

dim other as product=ctype(obj,product)

if me=other then return 0 else return 1

end function

Is that what you mean?

Eric





Nov 21 '05 #6
Sorry if I wasn't clear on this...You'll still have to compare the
individual properties in your "CompareTo" code, I was just suggesting
that you use the IComparable interface as a place to implement that
code.

Sorry for the confusion..

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
hmmm, I dont think that will still work because I am trying to compare the Properties of the class to see if they have been changed... here is some more code hope it helps...

mProduct = New Mountaire.Database.Product(txtPLUnumber.Text, mCurrentFacility.Location)

mProduct_Original = New Mountaire.Database.Product(txtPLUnumber.Text, mCurrentFacility.Location)

populateProductFields(mProduct)

This first bit creates 2 new product objects. The populateproductfields populates text fields and boxes with the information on that product.

When the user moves off this particular product I need to verify that no changes were made. The easiest way for me to do this is to compare the properties of both product objects to see if they are different.

there are 45 or so properties of the product class that need to be compared. If I understand you right if I compare the 2 alone it will still work?

ie

private function CompareTo(by val obj as object) as integer implements icomparable.compareto

dim other as product=ctype(obj,product)

if me=other then return 0 else return 1

end function

Is that what you mean?

Eric


Nov 21 '05 #7
no worries. Then we are back to reflection. I can get all the property names
from reflection, In fact I already have that code done. Do you know how I
can use that information to test the properties of the specific instances?
Dim productProperties() As PropertyInfo =
pType.GetProperties(BindingFlags.Public Or BindingFlags.Instance)

'Dim oProductProperties() As PropertyInfo =
poType.GetProperties(BindingFlags.GetProperty)

Dim PropertyItem As PropertyInfo

For Each PropertyItem In productProperties

With PropertyItem

Debug.WriteLine(PropertyItem.Name)

if mproduct.propertyitem=mOriginalProduct.propertyite m then
isDifferent=true

End With

Next

of course propertyitem doesnt work...do you know how I could use it to make
it work?


--
--Eric Cathell, MCSA
"Brian Swanson" <pu*******@gREMOVETHISmail.com> wrote in message
news:us**************@tk2msftngp13.phx.gbl...
Sorry if I wasn't clear on this...You'll still have to compare the
individual properties in your "CompareTo" code, I was just suggesting that
you use the IComparable interface as a place to implement that code.

Sorry for the confusion..

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
hmmm, I dont think that will still work because I am trying to compare
the Properties of the class to see if they have been changed... here is
some more code hope it helps...

mProduct = New Mountaire.Database.Product(txtPLUnumber.Text,
mCurrentFacility.Location)

mProduct_Original = New Mountaire.Database.Product(txtPLUnumber.Text,
mCurrentFacility.Location)

populateProductFields(mProduct)

This first bit creates 2 new product objects. The populateproductfields
populates text fields and boxes with the information on that product.

When the user moves off this particular product I need to verify that no
changes were made. The easiest way for me to do this is to compare the
properties of both product objects to see if they are different.

there are 45 or so properties of the product class that need to be
compared. If I understand you right if I compare the 2 alone it will
still work?

ie

private function CompareTo(by val obj as object) as integer implements
icomparable.compareto

dim other as product=ctype(obj,product)

if me=other then return 0 else return 1

end function

Is that what you mean?

Eric

Nov 21 '05 #8
I found the information I was looking for..if you are interested here it
is...

myClass.GetType.GetProperty("PropertyName").getval ue(myclass,nothing)

that will return the value of that property. You can then use the following
code to loop through the properties of your class...

For Each PropertyItem In productProperties

With PropertyItem

end with

next
--
--Eric Cathell, MCSA

"Brian Swanson" <pu*******@gREMOVETHISmail.com> wrote in message
news:us**************@tk2msftngp13.phx.gbl...
Sorry if I wasn't clear on this...You'll still have to compare the
individual properties in your "CompareTo" code, I was just suggesting that
you use the IComparable interface as a place to implement that code.

Sorry for the confusion..

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
hmmm, I dont think that will still work because I am trying to compare
the Properties of the class to see if they have been changed... here is
some more code hope it helps...

mProduct = New Mountaire.Database.Product(txtPLUnumber.Text,
mCurrentFacility.Location)

mProduct_Original = New Mountaire.Database.Product(txtPLUnumber.Text,
mCurrentFacility.Location)

populateProductFields(mProduct)

This first bit creates 2 new product objects. The populateproductfields
populates text fields and boxes with the information on that product.

When the user moves off this particular product I need to verify that no
changes were made. The easiest way for me to do this is to compare the
properties of both product objects to see if they are different.

there are 45 or so properties of the product class that need to be
compared. If I understand you right if I compare the 2 alone it will
still work?

ie

private function CompareTo(by val obj as object) as integer implements
icomparable.compareto

dim other as product=ctype(obj,product)

if me=other then return 0 else return 1

end function

Is that what you mean?

Eric

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MIchael McDowell | last post by:
Anyidea how this might be done using and XML Web Service and the XMLSerialzation class? A pointer to an online example would be deeply appreciated. Thankyou in advance, Michael McD
9
by: Kenneth Baltrinic | last post by:
What is the best way to determine if two objects are of the same type? The 'is' operator is not precise enough. Currently I am using Obj1.GetType().Name == Obj2.GetType().Name but it seems to me...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
7
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
10
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
6
by: Ladislav Andel | last post by:
Hi, what would be the most efficient way to do following? I have a list of dictionaries taken from DB e.g. dblist = and list of object instances in memory(it's just for example) which are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.