19 januari 2007

Looping through the properties of a self-defined object

If you want log something it can be usefull to print out the properties of a object. When you define a object yourself the code below can be usefull.

This function prints out each property of the object

Public Overrides Function toString() As String
'create Stringbuilder
Dim sbReturn As StringBuilder = New StringBuilder(Me.GetType().Name) sbReturn.Append(" :")
For Each PropertyItem As PropertyInfo In Me.GetType().GetProperties()
'add data
sbReturn.Append(PropertyItem.Name)
sbReturn.Append("= ")
sbReturn.Append(PropertyItem.GetValue(Me, Nothing).ToString())
 sbReturn.Append(", ")
Next
'return
Return Trim(sbReturn.ToString())
End Function

Make sure u import System.Reflection

Imports System.Reflection

Dit artikel gaat over: , , , . Looping through the properties of a self-defined object werd geschreven door Tijs.
Er zijn al 2 reacties.

Reacties

William

William schreef:
05/02/07

Is there anyway to modify the property looping function to loop through child objects? Currently gives a "object not initiated" error.

tijs

tijs schreef:
05/02/07

Feel free to modify it. I 'll have a look at it, but I'm really busy.

Reageer velden gemarkeerd met een sterretje zijn verplicht

*