Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get all property values from object / class

Status
Not open for further replies.

ispeaker

Programmer
Dec 29, 2005
15
US
I have a collection of objects. I can successfully loop through the collection, and print the value of any property by explicitly stating the property such as obj.ProductGID will print out the value of the current object's ProductGID property.

Looking for a way to print out the values without having to explicitly stating each one. The code below will print the names of the properties, but I don't know how to get the value.

Can anyone help?

Code:
  Dim prop As System.Reflection.PropertyInfo
  For Each prop In obj.GetType.GetProperties()
    Debug.Write(prop.Name & " ^^ ")
  Next prop
  Debug.WriteLine(vbCrLf)
 
ANSWER:

Put this into any collection class, and it will loop through every object in the collection and print the value of each object.

The code between the ==== lines is the code that gets the value of the property.

Code:
Public Shared Sub PrintValues(ByVal ItemList As IEnumerable)
Dim myEnumerator As System.Collections.IEnumerator = _
ItemList.GetEnumerator()
Dim obj As objPartsPricing.Part
Dim Value
While myEnumerator.MoveNext()
obj = myEnumerator.Current
'==========================================================
Dim prop As System.Reflection.PropertyInfo
For Each prop In obj.GetType.GetProperties()
Debug.Write(prop.GetValue(obj, prop.GetIndexParameters) & " -- ")
Next prop
Debug.WriteLine(vbCrLf)
'==========================================================
End While
Debug.WriteLine("")
End Sub
 
Thank you.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Your welcome, Chrissie1. Watch out for one thing: if one of the properties is another object or collection, it won't be able to handle it. I'm working on it. Will post solution.
 
To prevent the script from generating an error when a property of one class is another class, use this:


Code:
If Not prop.PropertyType.IsClass Then . . .
 
I tried this, and noticed that if a property is of type STRING nothing is returned. Did I miss something?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top