?
Solved

Casting numbers to variable names

Posted on 2006-03-21
3
Medium Priority
?
202 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
0
Comment
Question by:kasiencja
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 16250986
You need to keep an array of the component, then

 txtMaxTries[location].setEnabled(value);
0
 
LVL 92

Expert Comment

by:objects
ID: 16251232
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);
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16283181
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question