comparing fields in 2 different instances of the same object

  • Thread starter Thread starter ECathell
  • Start date Start date
E

ECathell

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?
 
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
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?
 
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
 
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?
 
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 said:
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?
 
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
 
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..
 
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.propertyitem then
isDifferent=true



End With



Next



of course propertyitem doesnt work...do you know how I could use it to make
it work?
 
I found the information I was looking for..if you are interested here it
is...

myClass.GetType.GetProperty("PropertyName").getvalue(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
 

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