Looping through the properties of a class

mcostello

Member
Joined
Dec 12, 2005
Messages
6
Programming Experience
5-10
Hi,
Can anyone tell me how to loop through the properties of a class.

E.g. I need to write to a change log the old values and the new values for a specific instance of a members class.
I need to create a string from the values
e.g. name1,address1,address2 etc
but I want to avoid having to do
objmember.name & "," & objmember.address1 etc

I presume you can loop through the properties similar to

Dim item as blah

for each item in objmember
do something
next

My problem is what to define item as.

Any and all help and comments appreciated

Mark
 
Here's a fairly simple example. You may want to make it more sophistcated as simply converting each proprty value to a string may not be sufficient depending on the types involved.
VB.NET:
    Private Sub GetPropertyValues()
        Dim properties As Reflection.PropertyInfo() = Me.GetType().GetProperties()

        For Each [property] As Reflection.PropertyInfo In properties
            MessageBox.Show(String.Format("Me.{0} = {1}", _
                                          [property].Name, _
                                          [property].GetValue(Me, Nothing)))
        Next
    End Sub
 
Back
Top