locked
Loop through object properties? RRS feed

  • Question

  • User-839293131 posted

    Hello - lets say I create a basic vb class object with a number of public properties.

    If I create an instance of this class, is it possible to loop through the properites of this object?

    e.g.

    Dim person as New Person

    For each pr as property in person

    Response.Write(pr.propertyName)

    That sort of thing?

    Thanks

    Sunday, April 13, 2008 5:38 AM

Answers

  • User214117797 posted

    If I create an instance of this class, is it possible to loop through the properites of this object?
     

    You will need to use reflection to get a list of properties that exist in a class.

    something like this

     

            Dim myType As Type = GetType(Person)
            Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
            For Each p As System.Reflection.PropertyInfo In properties
                Response.Write(p.Name)
            Next
     
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 13, 2008 6:22 AM

All replies

  • User214117797 posted

    If I create an instance of this class, is it possible to loop through the properites of this object?
     

    You will need to use reflection to get a list of properties that exist in a class.

    something like this

     

            Dim myType As Type = GetType(Person)
            Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
            For Each p As System.Reflection.PropertyInfo In properties
                Response.Write(p.Name)
            Next
     
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 13, 2008 6:22 AM
  • User-839293131 posted

    Perfect!

    Thanks.

    Sunday, April 13, 2008 7:01 AM
  • User-1752813390 posted

    Yes thanks,

    this slipped my mind and i appreciate the answer

    Thursday, November 20, 2008 11:17 AM