Re: Iterate through the class properties

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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@xxx> wrote in message
news:uCEe43I$HHA.4568@xxxxxxxxxxxxxxxxxxxxxxx

"John Wright" <riley_wrightx@xxxxxxxxxxx> wrote in message
news:OqpCurI$HHA.5980@xxxxxxxxxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: Iterate through the class properties
    ... 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. ... dim drProperty as DataRow = propertyChanges.Rows.Find ... Anyone have any ideas how to access the classes properties like this? ... You get an instance of the object and apply the reflection methods to obtain the properties. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Iterate through the class properties
    ... and have it reset the property to the previous value. ... Private Sub UndoChange (PropertyName as string) ... dim drProperty as DataRow = propertyChanges.Rows.Find ... dim obj as new object ' The object to search ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Can I reset a password or remove a username?
    ... it appears I must know the password answer to reset the ... passwordAnswer As String _ ... If you, as an admin, need to do this, Code against the Membership ... One gotcha. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Can I reset a password or remove a username?
    ... passwordAnswer As String _ ... "Gregory A. Beamer" wrote in message ... I will have to peruse my code base and find a sample, as I have already created a fairly simple admin page to reset a user's password. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: copying objects
    ... > (commit and reset) into another method named Clone(). ... > currVal and newVal will be simple types (ie. long, string, date...) though ...
    (microsoft.public.dotnet.languages.csharp)