473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Iterate through the class properties

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
5 1895
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
I'm going a little crazy trying to learn how to use arrays as properties in VBScript classes. Hopefully someone can help. First, I can't figure out whether it's possible to iterate through the...
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
14
by: Jan Nielsen | last post by:
Hi In Microsoft Access it is possible to write code like this Sub test() DoCmd.OpenForm "TestForm", acDesign Dim a As Control For Each a In Forms("TestForm").Controls Do stuff Next End Sub
16
by: Crirus | last post by:
I have a class. I need to write a routine in this class, that loop through it's members (in a instance of the class) and concatenate all members values as string. I need to filter does members...
1
by: hazz | last post by:
i'd like to iterate through the public properties of a class. eg. for i = 0 to someArrayList.Count for j = first public property of a Class to the Last public property of a Class '????? if...
2
by: | last post by:
I'm writing web applications. I build and extend a lot of custom objects, and in the course of debugging my apps I invariably find myself writing a lot of junky code: Response.Write("Title: " +...
3
by: toton | last post by:
Hi, I have a container class, and I want to iterate over a portion of the container class while I insert/remove item from it. Noting down the present location & constructing iterator from there is...
1
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, Is there a way ( perhaps throgh refleksion) to iterate through the colors of the System.Drawing.Color struct. foreach (Color color in .... i dont know.... regards Jesper
4
by: Peted | last post by:
Hi I need to iterate through a winforms visible datagrid, in a legacy application, to update it a little bit to change the background colours of the grid. This seems to be more dificult than...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.