Re: print out object variable values



try using reflection...iterate through the properties in the method then
print

see example

public String toString()
{
StringBuffer buff = new StringBuffer();

Field[] fields = this.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
try
{
buff.append(fields[i].getName())
.append("\t=>\t")
.append(fields[i].get(this))
.append("\n");

//System.out.println(fields[i].getName() + " : values: " +
// fields[i].get(this));
}
catch (IllegalAccessException ex)
{
ex.printStackTrace(System.out);
}
catch (IllegalArgumentException ex)
{
ex.printStackTrace(System.out);
}

}
return buff.toString();

}

"polilop" <fmatosic@@inet.hr> wrote in message
news:ds1pam$3kk$1@xxxxxxxxxxxxxxxxx
I have a class with 50 string variables.
I need to see in my main program which ones are set to some values.
To do this i need to print them to the screen. I'm getting fed up writting
System.out.println(myObject.getThisString);
System.out.println(myObject.getThatString)
.....
As i have to do this few times.

Is there an easy way to print all the values in an object?

Thx




.



Relevant Pages

  • Re: print out object variable values
    ... public String toString() ... StringBuffer buff = new StringBuffer; ... catch (IllegalAccessException ex) ...
    (comp.lang.java)
  • Re: Databinding to a combo box: Newbie
    ... Override the ToString function to return the 'look' that you want the ... The combobox will display the ToString output but the Selected item ... public struct stTuple ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ToString Debug vs Display strategies & preferences
    ... IFormattable implementation as well as ToString() override. ... public string FirstName ... public string ToString(string fmt, IFormatProvider fp) ... How about generalized debugging ToString stratagies? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: this.toString
    ... > I am a java beginner. ... > keyword makes an implicit call to Objects toString method as in the ... could have picked public String converThisObjectToAString() but I guess they ...
    (comp.lang.java.programmer)
  • Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET
    ... >public string getEmail ... which you try to cast by calling .ToString- but you're calling that ... can't just call a .ToString() on either. ... VB.NET probably shields you from those programming errors by some ...
    (microsoft.public.dotnet.languages.csharp)