473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

print out object variable values

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
Feb 4 '06 #1
2 22199
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:ds**********@ss405.t-com.hr...
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

Feb 6 '06 #2
Thx made my work easier

"Lans Redmond" <si********@earthlink.net> wrote in message
news:4w******************@newsread2.news.atl.earth link.net...
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:ds**********@ss405.t-com.hr...
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


Feb 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Krasnyk | last post by:
Hi, How can I print variable values by their names? For example: import sys for str in dir(sys): print str Will be printed only variable names, but I need values from list of variable...
3
by: babu | last post by:
Hello All, How are you? I am facing an issue to print all the form control values (including datagrid values). Is there a way to print all the control values in VB.NET. If the grid...
1
by: Deano | last post by:
Just recreated my relationships in the Relationships window. Trying to print and immediately our old friend 'Object variable or With Block variable not set' pops up. I've tried compacting and...
8
by: deko | last post by:
Is there a rule of thumb for quoting variable values? For example, do I have to put all string number values in quotes? strAbc = "3" ? What about long, int and byte? I assume these are NOT...
4
by: psbasha | last post by:
Hi, How to check the class attributes/member variable values while debuging in 'PythonWin'?. I need to watch all the member varibales values of a class ,given the object or etc. Thanks PSB
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
5
by: laredotornado | last post by:
Hi, I have this alert statement alert(myObj); which when executed, alerts "", or something similar. Is there a way I can see a list of attributes and values instead or do I have to write a...
0
by: bharathi228 | last post by:
hi, iam doning one vb.net application.it is windows application.here iam having 6 class libraries and all dlls are added as reference to another module.the main module is CEMDAS. The referenced...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.