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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print all property values of an object

Status
Not open for further replies.

rrhandle

Programmer
Joined
Dec 26, 2001
Messages
193
Location
US
I have an object named oDog. The object has numerous properties like: Breed, Color, Age, Weight, etc.
Once the properties are set, I would like to print all the values. Is there a way to do this without writing a Response.WriteLine statement for each property?

Thanks,

The only thing worse than being alone, is wishing you were.
 
You would write C# code like this. VB.NET code would be similar.
Code:
Type myType = typeof(oDog);
PropertyInfo[] pi = myType.GetProperties(BindingFlags.Public    
      | BindingFlags.Instance);

for (int i=0; i < myPropertyInfo.Length; i++)
{
    PropertyInfo myPropInfo = (PropertyInfo)pi[i];
    Console.WriteLine("The property name is {0}.", myPropInfo.Name);
    Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
    Console.WriteLine("The property value is {0}.", myPropInfo.GetValue(oDog, null));
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top