locked
Programmatically Ignore Inherited Properties while iterating through object properties RRS feed

  • Question

  • User1920326549 posted

    Hi All,

    I'm currently running a loop to iterate through an object properties:

    Public Class Notification
        Inherits CommonProp
        Public Property NotificationID As Integer
        Public Property Message As String
        Public Property ReceivedDateTime As DateTime
    End Class

    However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.

    Is there anyway of ignoring the CommonProp properties?

    Please kindly assist.

    Thank you in advance.

    Warmest Regards,

    leroylll


    Saturday, October 16, 2010 10:19 PM

Answers

  • User1224194097 posted

    However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.

    You can use Reflection and check if the Declaring Type and Reflecting Type are same. 

    Public Class ParentClass
        Private m_Name As String
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property
    End Class
    Public Class ChildClass
        Inherits ParentClass
        Private m_Number As String
        Public Property Number() As String
            Get
                Return m_Number
            End Get
            Set(ByVal value As String)
                m_Number = value
            End Set
        End Property
    End Class
    
    
    Dim cls As New ChildClass()
    cls.Number = "Test Number"
    Dim properties = cls.GetType().GetProperties() _
                                    .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList()
    'Get Values here
    Dim values = (From a In properties Select a.GetValue(cls, Nothing)).ToArray()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 17, 2010 12:25 AM
  • User1920326549 posted

    Just to add on, i found this code on http://snipplr.com/view/4648/add-item-to-array:

    1. // Add an item to the end of an existing array
    2. string[] ar1 = new string[] {"I", "Like", "To"}
    3.  
    4. // create a temporary array with an extra slot
    5. // at the end
    6. string[] ar2 = new string[ar1.Length + 1];
    7.  
    8. // add the contents of the ar1 to ar2
    9. // at position 0
    10. ar1.CopyTo(ar2, 0);
    11.  
    12. // add the desired value
    13. ar2.SetValue("Code.", ar1.Length);
    14.  
    15. // overwrite ar1 with ar2 and voila!
    16. // the contents of ar1 should now be {"I", "Like", "To", "Code."}
    17. ar1 = ar2;


    Hope it will be useful to those reading.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 21, 2010 10:19 AM

All replies

  • User1224194097 posted

    However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.

    You can use Reflection and check if the Declaring Type and Reflecting Type are same. 

    Public Class ParentClass
        Private m_Name As String
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property
    End Class
    Public Class ChildClass
        Inherits ParentClass
        Private m_Number As String
        Public Property Number() As String
            Get
                Return m_Number
            End Get
            Set(ByVal value As String)
                m_Number = value
            End Set
        End Property
    End Class
    
    
    Dim cls As New ChildClass()
    cls.Number = "Test Number"
    Dim properties = cls.GetType().GetProperties() _
                                    .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList()
    'Get Values here
    Dim values = (From a In properties Select a.GetValue(cls, Nothing)).ToArray()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 17, 2010 12:25 AM
  • User1920326549 posted

    However, when iterating through these properties, I do not want to iterate through the inherited CommonProp properties.

    How are you iterating through properties? Are you using Reflection? If so, you can check if the Declaring Type and Reflecting Type are same.


    1. Public Class ParentClass  
    2.     Private m_Name As String  
    3.     Public Property Name() As String  
    4.         Get  
    5.             Return m_Name  
    6.         End Get  
    7.         Set(ByVal value As String)  
    8.             m_Name = value  
    9.         End Set  
    10.     End Property  
    11. End Class  
    12. Public Class ChildClass  
    13.     Inherits ParentClass  
    14.     Private m_Number As String  
    15.     Public Property Number() As String  
    16.         Get  
    17.             Return m_Number  
    18.         End Get  
    19.         Set(ByVal value As String)  
    20.             m_Number = value  
    21.         End Set  
    22.     End Property  
    23. End Class  
    Public Class ParentClass
        Private m_Name As String
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property
    End Class
    Public Class ChildClass
        Inherits ParentClass
        Private m_Number As String
        Public Property Number() As String
            Get
                Return m_Number
            End Get
            Set(ByVal value As String)
                m_Number = value
            End Set
        End Property
    End Class
    
    1. Dim cls As New ChildClass()  
    2.         cls.Number = "Test Number"  
    3.         Dim properties = cls.GetType().GetProperties() _  
    4.                                 .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList()  
    5.         'Get Value here  
    6.         Dim strNumber = properties.First().GetValue(cls, Nothing)  
    Dim cls As New ChildClass()
            cls.Number = "Test Number"
            Dim properties = cls.GetType().GetProperties() _
                                    .Where(Function(a) a.DeclaringType.Equals(a.ReflectedType)).ToList()
            'Get Value here
            Dim strNumber = properties.First().GetValue(cls, Nothing)

    Correct me if I had understood your requirement wrong.

    yup.... im intending to use Reflection to iterate through the properties of the class Notification (without the CommonProp properties) and insert the property values into a paramarrray.


    Sunday, October 17, 2010 4:16 AM
  • User1224194097 posted

    im intending to use Reflection to iterate through the properties of the class Notification (without the CommonProp properties) and insert the property values into a paramarrray.

    Did you try my code? 

    Sunday, October 17, 2010 10:21 PM
  • User1920326549 posted

    im intending to use Reflection to iterate through the properties of the class Notification (without the CommonProp properties) and insert the property values into a paramarrray.

    Did you try my code? 


    Yup... thanks.... it worked.

    Tuesday, October 19, 2010 11:55 AM
  • User1920326549 posted

    Just to add on, i found this code on http://snipplr.com/view/4648/add-item-to-array:

    1. // Add an item to the end of an existing array
    2. string[] ar1 = new string[] {"I", "Like", "To"}
    3.  
    4. // create a temporary array with an extra slot
    5. // at the end
    6. string[] ar2 = new string[ar1.Length + 1];
    7.  
    8. // add the contents of the ar1 to ar2
    9. // at position 0
    10. ar1.CopyTo(ar2, 0);
    11.  
    12. // add the desired value
    13. ar2.SetValue("Code.", ar1.Length);
    14.  
    15. // overwrite ar1 with ar2 and voila!
    16. // the contents of ar1 should now be {"I", "Like", "To", "Code."}
    17. ar1 = ar2;


    Hope it will be useful to those reading.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 21, 2010 10:19 AM