Reflection issue

E

ECathell

I have a routine that compares 2 intances of the same object to see if there are any changes. The problem is that even though there are no changes, it tells me there are. I have even tried running a new routine that shows a messagebox with both objects' property values. below is my code.


this is the actual comparison code:
Dim Properties() As PropertyInfo = pType.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
Dim PropertyItem As PropertyInfo
For Each PropertyItem In Properties
If ischanged = False Then

With PropertyItem
Dim o1 As New Object''These are just for testing and will be returned to refactored code
Dim o2 As New Object''These are just for testing and will be returned to refactored code
o1 = .GetValue(obj, Nothing)''These are just for testing and will be returned to refactored code
o2 = .GetValue(obj_original, Nothing)''These are just for testing and will be returned to refactored code
'TODO Comment These Out
Debug.WriteLine(String.Format("Property Name: {0} PropertyType:{1}", PropertyItem.Name, PropertyItem.PropertyType))
Debug.WriteLine(String.Format(" Name {0}", .Name))
Debug.WriteLine(String.Format(" New Value {0}", o1))
Debug.WriteLine(String.Format("Original Value {0}", o2))
'Debug.WriteLine(String.Format(" isChanged {0}", ischanged.ToString))
Debug.WriteLine(Environment.NewLine)
If Not o1.Equals(o2) Then

ischanged = True
End If


End With
End If
Next

This is the code that I validate with first: it displays the information properly.

Private Shared Sub validateValues(ByVal obj As Object, ByVal obj_original As Object, ByVal ptype As Type)
Dim s As New StringBuilder

Dim Properties() As PropertyInfo = ptype.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
Dim PropertyItem As PropertyInfo
For Each PropertyItem In Properties
With PropertyItem
s.AppendFormat("Property Name:{0} CurrentValue:{1} OriginalValue:{1}", .Name, .GetValue(obj, Nothing), ..GetValue(obj_original, Nothing))
s.Append(Environment.NewLine)

End With
Next
MessageBox.Show(s.ToString)

End Sub
 
G

Guest

That's because the line:

If Not o1.Equals(o2) Then
ischanged = True
End If

is checking for reference equality, and not value equality. For example:

Dim myObj1 As Object = New Object()
Dim myObj2 As Object = New Object()

'Will return false because the references point to different object
MessageBox.Show(myObj1.Equals(myObj2))

myObj1 = myObj2

'Will return true because the references point to the same object
MessageBox.Show(myObj1.Equals(myObj2))
 
G

Guest

That's because the line:

If Not o1.Equals(o2) Then
ischanged = True
End If

is checking for reference equality, and not value equality. For example:

Dim myObj1 As Object = New Object()
Dim myObj2 As Object = New Object()

'Will return false because the references point to different object
MessageBox.Show(myObj1.Equals(myObj2))

myObj1 = myObj2

'Will return true because the references point to the same object
MessageBox.Show(myObj1.Equals(myObj2))
 
G

Guest

Honestly, that depends on what the underlying type actually is. The Equals()
method in System.Object checks for reference equality. However, in a lot of
reference types derived from System.Object, the actual type overrides the
Equals() method to check for value equality (like System.String).

What are the System.Type values are you expecting on these properties? You
can try casting them to those specified types and then checking the values
using that types Equals() method (assuming it checks for value equality).
 
E

ECathell

I am using reflection to view the property values of a custom class. The
strange thing is on my non-complex objects this already works fine. It is
just on this class for now.All my classes have an abstract base and a
concrete class. However, this class has enumerations attached. Maybe that is
where my error is because my other classes dont do this...


Region "PrePack Enumeration"
Public Enum PrePackTypes
ICEPACK
TRAYPACK
End Enum
' Private mPrepack As Boolean
Private xPrepack As PrePackTypes
Public Shadows Property Prepack() As PrePackTypes
Get
Select Case MyBase.PrePack
Case True
xPrepack = PrePackTypes.TRAYPACK
Case False
xPrepack = PrePackTypes.ICEPACK

End Select

Return xPrepack
End Get
Set(ByVal Value As PrePackTypes)

xPrepack = Value
Select Case xPrepack
Case PrePackTypes.ICEPACK
MyBase.PrePack = False
Case PrePackTypes.TRAYPACK
MyBase.PrePack = True
End Select
End Set
End Property
#End Region

#Region "TareType Enumeration"
Public Enum TareTypes
STANDARD
PERCENTAGE
End Enum

'Private mTareType As Boolean
Private xTaretype As TareTypes
Public Shadows Property TareType() As TareTypes
Get
Select Case MyBase.TareType
Case True
xTaretype = TareTypes.PERCENTAGE
Case False
xTaretype = TareTypes.STANDARD
End Select
Return xTaretype

End Get
Set(ByVal Value As TareTypes)
xTaretype = Value
Select Case xTaretype
Case TareTypes.PERCENTAGE
MyBase.TareType = True
Case TareTypes.STANDARD
MyBase.TareType = False
End Select
End Set
End Property
'
#End Region

#Region "StorageTypes Enumeration"
Public Enum StorageTypes
REFRIGERATED
FROZEN
End Enum
'Private mStorage As String
Private xStorage As StorageTypes
Public Shadows Property Storage() As StorageTypes

Get
Select Case mStorage
Case "REFRIGERATED"
xStorage = StorageTypes.REFRIGERATED
Case "FROZEN"
xStorage = StorageTypes.FROZEN
End Select

Return xStorage

End Get
Set(ByVal Value As StorageTypes)
xStorage = Value
Select Case xStorage
Case StorageTypes.FROZEN
mStorage = "FROZEN"
Case StorageTypes.REFRIGERATED
mStorage = "REFRIGERATED"
End Select

End Set
End Property
#End Region

#Region "DateTypes Enumeration"
Public Enum DateTypes
Code:
[PACKED]
[SELL_BY]
[JULIAN]
End Enum

'Private mDateType As String
Private xDateType As DateTypes
Public Shadows Property DateType() As DateTypes
Get
Select Case mDateType
Case "CODE"
xDateType = DateTypes.CODE
Case "SELL-BY"
xDateType = DateTypes.SELL_BY
Case "PACKED"
xDateType = DateTypes.PACKED
Case "JULIAN"
xDateType = DateTypes.JULIAN

End Select
Return xDateType

End Get
Set(ByVal Value As DateTypes)
xDateType = Value
Select Case xDateType
Case DateTypes.CODE
mDateType = "CODE"
Case DateTypes.PACKED
mDateType = "PACKED"
Case DateTypes.SELL_BY
mDateType = "SELL-BY"
Case DateTypes.JULIAN
mdatetype = "JULIAN"
End Select

End Set
End Property


#End Region

#Region "WeightTypes Enumeration"
Public Enum WeightTypes
[FIXED]
[CATCH]
[KEYED]
End Enum
'Private mWeightType As String
Private xWeightType As WeightTypes
Public Shadows Property WeightType() As WeightTypes
Get

Select Case mWeightType
Case "FIXED"
xWeightType = WeightTypes.FIXED

Case "CATCH"
xWeightType = WeightTypes.CATCH

Case "KEYED"
xWeightType = WeightTypes.KEYED

End Select

Return xWeightType

End Get
Set(ByVal Value As WeightTypes)
xWeightType = Value
Select Case xWeightType
Case WeightTypes.CATCH
mWeightType = "CATCH"
Case WeightTypes.FIXED
mWeightType = "FIXED"
Case WeightTypes.KEYED
mWeightType = "KEYED"
End Select

End Set
End Property
'
#End Region
 
E

ECathell

I fixed it...I think it had an issue with my shadows properties...changed
their name and voila....

Thanks for the help....
 

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

Top