Alex
Alex

Reputation: 3423

How to iterate over the properties of an object in vb.net?

I'm debuging an complex calculation object in my project, and I'd like to show its various and many properties in a textbox, to make my tests easier.

Can I do something like

for each p as someKindOfProperty in MyObject1
  debug.print(p.name & " - " & debug.print p.value)
  textbox1.text = textbox1.text & vbcrlf & p.name & " - " & p.value
next

???

How?

Upvotes: 2

Views: 755

Answers (1)

Bala R
Bala R

Reputation: 108967

Dim props As PropertyInfo() = GetType(Color).GetProperties(BindingFlags.[Static] Or BindingFlags.[Public])

For Each prop As PropertyInfo In props
    Dim o As Object = prop.GetValue(Nothing, Nothing)
    If o IsNot Nothing Then
        textbox1.Text = Textbox1.text + Constants.vbcrlf + prop.Name + " - " + o.ToString()
    End If
Next

Upvotes: 5

Related Questions