Reference to an object's property with a string

npearson99

New member
Joined
Mar 24, 2008
Messages
1
Programming Experience
1-3
I'm trying to loop through some objects properties.

Object c has these properties:

c.property1
c.property2
c.property3
c.property4
c.property5
c.property6
c.property7
...to
c.property100

I want to get the value from each property. So I want to do something like this:
for i = 1 to 101
myVar = c["property" + i]
' do something cool
next

that code with the c[] is actually actionscript 3 code. I'm trying to do the same thing in VB.NET.

I hope this makes sense.

-Nate
 
Reflection.

Either

ForEach prop As System.Reflection.PropertyInfo In myObject.GetType.GetProperties()
value = myObject.GetType.InvokeMember(prop.Name, Reflection.BindingFlags.GetProperty,
Nothing, myObject, Nothing)
Next

Or

For x AsInteger = 0 To 100
value = myObject.GetType.InvokeMember("Property" +
CStr(x), Reflection.BindingFlags.GetProperty, Nothing, myObject, Nothing)
Next
 
That looks like a bad idea, plain and simple. It sounds to me like you should be exposing an array via a single read-only property. Reflection si something you should avoid unless it's required and here it simply isn't. If you really do have 100 properties then write 100 lines of code. We do live in the copy and paste era you know.
 
Back
Top