We help IT Professionals succeed at work.

Casting numbers to variable names

kasiencja
kasiencja asked
on
Medium Priority
216 Views
Last Modified: 2010-08-05
I'm not sure if casting is the right term in here but here is what I'm trying to figure out how to do...

What I’m doing right now:
    private void setBoxValue (int location, boolean value) // location can be a String it doesn’t really matter
    {
          switch(location) {
          case 0:
                txtMaxTries_0.setEnabled(value);
                break;
          case 1:
                txtMaxTries_1.setEnabled(value);
                break;
          case 2:
                txtMaxTries_2.setEnabled(value);
                break;
                // and so on
          }

    }

What I would like to be able to do:
    private void setBoxValue (int location, boolean value) // location can be a String it doesn’t really matter
    {
          txtMaxTries_(location).setEnabled(value);
    }


Thanks for your help,
Kasia
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
You need to keep an array of the component, then

 txtMaxTries[location].setEnabled(value);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you could use a Map with a String (or any class) location:

    private Map txtMaxTries = new HashMap();

    private void setBoxValue (String location, boolean value) // location can be a String it doesn’t really matter
    {
         JCheckBox txtMaxTry = (JCheckBox) txtMaxTries.get(location);  // assuming they are checkboxes, change type if not
         txtMaxTry.setEnabled(value);
    }
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.