Comparing the value of a field

T

tim

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:"
 
D

Daniel Pitts

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:"

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()
 
P

Patricia Shanahan

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:"

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
 
T

tim

Patricia said:
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?

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'.
 
D

Daniel Pitts

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'.

Why not use a HashMap instead of a class?
 
D

Daniel Pitts

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>();
 
T

tim

Daniel said:
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>();

Because that is the way it is set up.
 
J

John Ersatznom

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top