Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Comparing the value of a field

Thread view: 
tim@nocomment.com - 07 Jan 2007 21:26 GMT
If I want to change the value of the variable "count" using the
variable name "field_name" in the statement instead of "count", how do
I specify this?

String field_name = "count";

Instead of "count = 1;", I want to use something like
"field_name.valueOf() = 1:"
Daniel Pitts - 07 Jan 2007 21:42 GMT
> If I want to change the value of the variable "count" using the
> variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 4 lines]
> Instead of "count = 1;", I want to use something like
> "field_name.valueOf() = 1:"

There isn't really an easy or good way to do this.
You can use Reflection, but its a lot slower, and easier to add bugs.
If you REALLY must do it, look up Class.getFields()
Patricia Shanahan - 07 Jan 2007 22:02 GMT
> If I want to change the value of the variable "count" using the
> variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 4 lines]
> Instead of "count = 1;", I want to use something like
> "field_name.valueOf() = 1:"

It can be done, using reflection.

However, there are idioms in some languages that look like this, but are
better replaced by other Java features, not reflection.

What are you really trying to achieve? Why do you need this?

Patricia
tim@nocomment.com - 07 Jan 2007 22:31 GMT
> > If I want to change the value of the variable "count" using the
> > variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 11 lines]
>
> What are you really trying to achieve? Why do you need this?

I have a table that has user_id in one field and a list of fields
separated by a comma in another field. This cannot be changed or
redesigned! These fields are codes representing the fields that this
particular user can update.I want to put these fields into a Vector,
which I have already done, Then I want to iterate through the Vector
and use the fields as a key for a hash map which contains all of the
field codes and the actual field names. I will take these field names,
append an "_attr", which will be the name of that fields related
attribute field, and change the value from "readonly='true' to
readonly='false'.
Daniel Pitts - 08 Jan 2007 01:00 GMT
> > > If I want to change the value of the variable "count" using the
> > > variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 22 lines]
> attribute field, and change the value from "readonly='true' to
> readonly='false'.

Why not use a HashMap instead of a class?
tim@nocomment.com - 08 Jan 2007 02:14 GMT
> > > > If I want to change the value of the variable "count" using the
> > > > variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 24 lines]
>
> Why not use a HashMap instead of a class?

What do you mean?
Daniel Pitts - 08 Jan 2007 17:58 GMT
> > > > > If I want to change the value of the variable "count" using the
> > > > > variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 26 lines]
>
> What do you mean?
Why access the fields in a object at all?  Why not just use a
HashMap<String, Boolean> that specifies what fields a user can read.
Better yet, have a Set<String> readOnlyFields = new HashSet<String>();
tim@nocomment.com - 08 Jan 2007 20:01 GMT
> > > > > > If I want to change the value of the variable "count" using the
> > > > > > variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 29 lines]
> HashMap<String, Boolean> that specifies what fields a user can read.
> Better yet, have a Set<String> readOnlyFields = new HashSet<String>();

Because that is the way it is set up.
Daniel Pitts - 08 Jan 2007 21:00 GMT
> > > > > > > If I want to change the value of the variable "count" using the
> > > > > > > variable name "field_name" in the statement instead of "count", how do
[quoted text clipped - 31 lines]
>
> Because that is the way it is set up.
And you've carved the source code into stone?
Change the way its set up, thats what I'm saying.
John Ersatznom - 08 Jan 2007 22:24 GMT
>>>>Why not use a HashMap instead of a class?
>>>
[quoted text clipped - 5 lines]
>
> Because that is the way it is set up.

With apologies to garbage men, uh I mean waste management personnel, and
environmentalists everywhere:

Reduce, Reuse, Refactor.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.