Iterate through the class properties

J

John Wright

I want to create a method in my class where I can pass in a property name
and have it reset the property to the previous value. I have the method
that stores the properties last value, now I need the class that can
interate through the properties and reset the value. Something like this


Private Sub UndoChange (PropertyName as string)
'Get the old value
dim drProperty as DataRow = propertyChanges.Rows.Find(PropertyName)

If Not IsNothing (drProperty) then
'PseduoCode
'iterate throught the properties and reset the property (this is
the code I need)
...find property
[Property].Value = drProperty.Item("OldValue"))

End if
End Sub

Anyone have any ideas how to access the classes properties like this?

John
 
L

Lloyd Sheen

John Wright said:
I want to create a method in my class where I can pass in a property name
and have it reset the property to the previous value. I have the method
that stores the properties last value, now I need the class that can
interate through the properties and reset the value. Something like this


Private Sub UndoChange (PropertyName as string)
'Get the old value
dim drProperty as DataRow = propertyChanges.Rows.Find(PropertyName)

If Not IsNothing (drProperty) then
'PseduoCode
'iterate throught the properties and reset the property (this is
the code I need)
...find property
[Property].Value = drProperty.Item("OldValue"))

End if
End Sub

Anyone have any ideas how to access the classes properties like this?

John

You need to use reflection. I have code but not where I am. Look up the
relection classes and it is easy. You get an instance of the object and
apply the reflection methods to obtain the properties. You can also set the
values of the properties using reflection.

Hope this helps
Lloyd Sheen
 
J

John Wright

Got it. I have to finish iteriating the PropertyType case statement to
include the types (unless someone else knows a better way). Here is the
code:

Public Overloads Sub UndoChange(ByVal PropertyName As String)

Try

'Find the row in the changes datatable with the property name

Dim drProperty As DataRow =
propertyChanges.Rows.Find(PropertyName)

'check to make sure we got something

If Not IsNothing(drProperty) Then

Dim userType As Type = Me.GetType

'Get the local property

Dim userProp As PropertyInfo =
userType.GetProperty(PropertyName)

Select Case userProp.PropertyType.Name.ToString

Case "String"

userProp.SetValue(Me,
drProperty.Item("OldValue").ToString, Nothing)

Case "Int32"

userProp.SetValue(Me, CInt(drProperty.Item("OldValue")),
Nothing)

End Select

'this has been reset, remove the row so we don't log a false
update

drProperty.Delete()

End If

Catch ex As Exception

End Try

End Sub

Lloyd Sheen said:
John Wright said:
I want to create a method in my class where I can pass in a property name
and have it reset the property to the previous value. I have the method
that stores the properties last value, now I need the class that can
interate through the properties and reset the value. Something like this


Private Sub UndoChange (PropertyName as string)
'Get the old value
dim drProperty as DataRow = propertyChanges.Rows.Find(PropertyName)

If Not IsNothing (drProperty) then
'PseduoCode
'iterate throught the properties and reset the property (this is
the code I need)
...find property
[Property].Value = drProperty.Item("OldValue"))

End if
End Sub

Anyone have any ideas how to access the classes properties like this?

John

You need to use reflection. I have code but not where I am. Look up the
relection classes and it is easy. You get an instance of the object and
apply the reflection methods to obtain the properties. You can also set
the values of the properties using reflection.

Hope this helps
Lloyd Sheen
 
J

Jack Jackson

I want to create a method in my class where I can pass in a property name
and have it reset the property to the previous value. I have the method
that stores the properties last value, now I need the class that can
interate through the properties and reset the value. Something like this


Private Sub UndoChange (PropertyName as string)
'Get the old value
dim drProperty as DataRow = propertyChanges.Rows.Find(PropertyName)

If Not IsNothing (drProperty) then
'PseduoCode
'iterate throught the properties and reset the property (this is
the code I need)
...find property
[Property].Value = drProperty.Item("OldValue"))

End if
End Sub

Anyone have any ideas how to access the classes properties like this?

John

dim obj as new object ' The object to search
dim fieldName as string = "xxx" 'Name of field

dim propDescColl As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(obj)
dim pd as PropertyDescriptor

pd = propDescColl.Find(fieldName, True)

The PropertyDescriptor has GetValue and SetValue methods.

I suspect that GetProperties is slow, so it would probably be a good
idea to only do it once and save the result.
 
C

Cor Ligthert[MVP]

John,

As it is about the DataTable with his rows and columns then there is a very
easy method because all item names or whatever are described in the columns.
Those are easy to itterate becouse these has by instance a columname,
however, even more properties in it.

Cor
 

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