By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

Iterate through the class properties

P: n/a
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
Sep 21 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I am not sure if this will help you but check this link out
http://msdn2.microsoft.com/en-us/library/ms162232.aspx


"John Wright" <ri***********@hotmail.comwrote in message
news:Oq**************@TK2MSFTNGP04.phx.gbl...
>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
Sep 21 '07 #2

P: n/a

"John Wright" <ri***********@hotmail.comwrote in message
news:Oq**************@TK2MSFTNGP04.phx.gbl...
>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

Sep 21 '07 #3

P: n/a
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" <a@b.cwrote in message
news:uC**************@TK2MSFTNGP02.phx.gbl...
>
"John Wright" <ri***********@hotmail.comwrote in message
news:Oq**************@TK2MSFTNGP04.phx.gbl...
>>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

Sep 21 '07 #4

P: n/a
On Fri, 21 Sep 2007 14:06:40 -0600, "John Wright"
<ri***********@hotmail.comwrote:
>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.
Sep 21 '07 #5

P: n/a
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

"John Wright" <ri***********@hotmail.comschreef in bericht
news:Oq**************@TK2MSFTNGP04.phx.gbl...
>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
Sep 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.