Reflection issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ECathell

    #1

    Reflection issue

    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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
    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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
    Debug.WriteLine (String.Format( " Name {0}", .Name))
    Debug.WriteLine (String.Format( " New Value {0}", o1))
    Debug.WriteLine (String.Format( "Original Value {0}", o2))
    'Debug.WriteLin e(String.Format (" isChanged {0}", ischanged.ToStr ing))
    Debug.WriteLine (Environment.Ne wLine)
    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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
    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(Enviro nment.NewLine)

    End With
    Next
    MessageBox.Show (s.ToString)

    End Sub
    --
    --Eric Cathell, MCSA
  • rmacias

    #2
    RE: Reflection issue

    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))

    "ECathell" wrote:
    [color=blue]
    > 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
    > 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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
    > Debug.WriteLine (String.Format( " Name {0}", .Name))
    > Debug.WriteLine (String.Format( " New Value {0}", o1))
    > Debug.WriteLine (String.Format( "Original Value {0}", o2))
    > 'Debug.WriteLin e(String.Format (" isChanged {0}", ischanged.ToStr ing))
    > Debug.WriteLine (Environment.Ne wLine)
    > 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
    > 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(Enviro nment.NewLine)
    >
    > End With
    > Next
    > MessageBox.Show (s.ToString)
    >
    > End Sub
    > --
    > --Eric Cathell, MCSA[/color]

    Comment

    • rmacias

      #3
      RE: Reflection issue

      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))

      "ECathell" wrote:
      [color=blue]
      > 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
      > 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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
      > Debug.WriteLine (String.Format( " Name {0}", .Name))
      > Debug.WriteLine (String.Format( " New Value {0}", o1))
      > Debug.WriteLine (String.Format( "Original Value {0}", o2))
      > 'Debug.WriteLin e(String.Format (" isChanged {0}", ischanged.ToStr ing))
      > Debug.WriteLine (Environment.Ne wLine)
      > 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
      > 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(Enviro nment.NewLine)
      >
      > End With
      > Next
      > MessageBox.Show (s.ToString)
      >
      > End Sub
      > --
      > --Eric Cathell, MCSA[/color]

      Comment

      • ECathell

        #4
        Re: Reflection issue

        So then how do i check for value equality?

        --
        --Eric Cathell, MCSA
        "rmacias" <rmacias@newsgr oup.nospam> wrote in message
        news:D16B1F16-8E33-4F04-95FD-BA2FD27CA76B@mi crosoft.com...[color=blue]
        > 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))
        >
        > "ECathell" wrote:
        >[color=green]
        >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
        >> 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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
        >> Debug.WriteLine (String.Format( " Name {0}", .Name))
        >> Debug.WriteLine (String.Format( " New Value {0}", o1))
        >> Debug.WriteLine (String.Format( "Original Value {0}", o2))
        >> 'Debug.WriteLin e(String.Format (" isChanged {0}",
        >> ischanged.ToStr ing))
        >> Debug.WriteLine (Environment.Ne wLine)
        >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
        >> 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(Enviro nment.NewLine)
        >>
        >> End With
        >> Next
        >> MessageBox.Show (s.ToString)
        >>
        >> End Sub
        >> --
        >> --Eric Cathell, MCSA[/color][/color]


        Comment

        • rmacias

          #5
          Re: Reflection issue

          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).

          "ECathell" wrote:
          [color=blue]
          > So then how do i check for value equality?
          >
          > --
          > --Eric Cathell, MCSA
          > "rmacias" <rmacias@newsgr oup.nospam> wrote in message
          > news:D16B1F16-8E33-4F04-95FD-BA2FD27CA76B@mi crosoft.com...[color=green]
          > > 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))
          > >
          > > "ECathell" wrote:
          > >[color=darkred]
          > >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
          > >> 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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
          > >> Debug.WriteLine (String.Format( " Name {0}", .Name))
          > >> Debug.WriteLine (String.Format( " New Value {0}", o1))
          > >> Debug.WriteLine (String.Format( "Original Value {0}", o2))
          > >> 'Debug.WriteLin e(String.Format (" isChanged {0}",
          > >> ischanged.ToStr ing))
          > >> Debug.WriteLine (Environment.Ne wLine)
          > >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
          > >> 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(Enviro nment.NewLine)
          > >>
          > >> End With
          > >> Next
          > >> MessageBox.Show (s.ToString)
          > >>
          > >> End Sub
          > >> --
          > >> --Eric Cathell, MCSA[/color][/color]
          >
          >
          >[/color]

          Comment

          • ECathell

            #6
            Re: Reflection issue

            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.TR AYPACK
            Case False
            xPrepack = PrePackTypes.IC EPACK

            End Select

            Return xPrepack
            End Get
            Set(ByVal Value As PrePackTypes)

            xPrepack = Value
            Select Case xPrepack
            Case PrePackTypes.IC EPACK
            MyBase.PrePack = False
            Case PrePackTypes.TR AYPACK
            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.PERCE NTAGE
            Case False
            xTaretype = TareTypes.STAND ARD
            End Select
            Return xTaretype

            End Get
            Set(ByVal Value As TareTypes)
            xTaretype = Value
            Select Case xTaretype
            Case TareTypes.PERCE NTAGE
            MyBase.TareType = True
            Case TareTypes.STAND ARD
            MyBase.TareType = False
            End Select
            End Set
            End Property
            '
            #End Region

            #Region "StorageTyp es 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 "REFRIGERAT ED"
            xStorage = StorageTypes.RE FRIGERATED
            Case "FROZEN"
            xStorage = StorageTypes.FR OZEN
            End Select

            Return xStorage

            End Get
            Set(ByVal Value As StorageTypes)
            xStorage = Value
            Select Case xStorage
            Case StorageTypes.FR OZEN
            mStorage = "FROZEN"
            Case StorageTypes.RE FRIGERATED
            mStorage = "REFRIGERAT ED"
            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.PACKE D
            Case "JULIAN"
            xDateType = DateTypes.JULIA N

            End Select
            Return xDateType

            End Get
            Set(ByVal Value As DateTypes)
            xDateType = Value
            Select Case xDateType
            Case DateTypes.CODE
            mDateType = "CODE"
            Case DateTypes.PACKE D
            mDateType = "PACKED"
            Case DateTypes.SELL_ BY
            mDateType = "SELL-BY"
            Case DateTypes.JULIA N
            mdatetype = "JULIAN"
            End Select

            End Set
            End Property


            #End Region

            #Region "WeightType s 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.FIX ED

            Case "CATCH"
            xWeightType = WeightTypes.CAT CH

            Case "KEYED"
            xWeightType = WeightTypes.KEY ED

            End Select

            Return xWeightType

            End Get
            Set(ByVal Value As WeightTypes)
            xWeightType = Value
            Select Case xWeightType
            Case WeightTypes.CAT CH
            mWeightType = "CATCH"
            Case WeightTypes.FIX ED
            mWeightType = "FIXED"
            Case WeightTypes.KEY ED
            mWeightType = "KEYED"
            End Select

            End Set
            End Property
            '
            #End Region

            --
            --Eric Cathell, MCSA
            "rmacias" <rmacias@newsgr oup.nospam> wrote in message
            news:BBED5CBD-3EE6-41F0-B1E6-635208956F3C@mi crosoft.com...[color=blue]
            > 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).
            >
            > "ECathell" wrote:
            >[color=green]
            >> So then how do i check for value equality?
            >>
            >> --
            >> --Eric Cathell, MCSA
            >> "rmacias" <rmacias@newsgr oup.nospam> wrote in message
            >> news:D16B1F16-8E33-4F04-95FD-BA2FD27CA76B@mi crosoft.com...[color=darkred]
            >> > 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))
            >> >
            >> > "ECathell" wrote:
            >> >
            >> >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
            >> >> 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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
            >> >> Debug.WriteLine (String.Format( " Name {0}",
            >> >> .Name))
            >> >> Debug.WriteLine (String.Format( " New Value {0}", o1))
            >> >> Debug.WriteLine (String.Format( "Original Value {0}", o2))
            >> >> 'Debug.WriteLin e(String.Format (" isChanged {0}",
            >> >> ischanged.ToStr ing))
            >> >> Debug.WriteLine (Environment.Ne wLine)
            >> >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
            >> >> 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(Enviro nment.NewLine)
            >> >>
            >> >> End With
            >> >> Next
            >> >> MessageBox.Show (s.ToString)
            >> >>
            >> >> End Sub
            >> >> --
            >> >> --Eric Cathell, MCSA[/color]
            >>
            >>
            >>[/color][/color]


            Comment

            • ECathell

              #7
              Re: Reflection issue

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

              Thanks for the help....


              --
              --Eric Cathell, MCSA
              "rmacias" <rmacias@newsgr oup.nospam> wrote in message
              news:BBED5CBD-3EE6-41F0-B1E6-635208956F3C@mi crosoft.com...[color=blue]
              > 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).
              >
              > "ECathell" wrote:
              >[color=green]
              >> So then how do i check for value equality?
              >>
              >> --
              >> --Eric Cathell, MCSA
              >> "rmacias" <rmacias@newsgr oup.nospam> wrote in message
              >> news:D16B1F16-8E33-4F04-95FD-BA2FD27CA76B@mi crosoft.com...[color=darkred]
              >> > 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))
              >> >
              >> > "ECathell" wrote:
              >> >
              >> >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
              >> >> 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_o riginal, 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.Na me, PropertyItem.Pr opertyType))
              >> >> Debug.WriteLine (String.Format( " Name {0}",
              >> >> .Name))
              >> >> Debug.WriteLine (String.Format( " New Value {0}", o1))
              >> >> Debug.WriteLine (String.Format( "Original Value {0}", o2))
              >> >> 'Debug.WriteLin e(String.Format (" isChanged {0}",
              >> >> ischanged.ToStr ing))
              >> >> Debug.WriteLine (Environment.Ne wLine)
              >> >> 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.GetProper ties(BindingFla gs.Public Or BindingFlags.In stance)
              >> >> 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(Enviro nment.NewLine)
              >> >>
              >> >> End With
              >> >> Next
              >> >> MessageBox.Show (s.ToString)
              >> >>
              >> >> End Sub
              >> >> --
              >> >> --Eric Cathell, MCSA[/color]
              >>
              >>
              >>[/color][/color]


              Comment

              Working...