Accessing variable names using String

I have to check up to 13 JCheckBox variables to see if they are checked or not, and I have the variables named: count1, count2, count3,...,count13. I wanted to know if there was a way to go through them in a while loop until I found the last one that was checked. I want to do the following:
int i = 1;
while([count+i].isSelected()) {
i++;
int foodCount = i;
//foodCount is stored in database
I am doing this for a pet store and I don't want to have 12 extra columns in the database - I just want to have one for the overall food count. Any ideas how I can dynamically create a variable name?

Put the JCheckBoxes in an arrayJCheckBox[] myCheckBoxes = {check1, check2, check3,...,check13};
JCheckBox someCheckBox = null;
for (int x=0; x<myCheckBoxes.length; x++) {
  if (myCheckBoxes[x].isSelected()) someCheckBox = myCheckBox[x];
}

Similar Messages

  • Variable name to string

    Hi, I'm really new to TestStand, so this is probably an easy question:
    I'm using TestStand 4.5 and I'm trying to convert the name of a variable, Locals.MaxArrayIndex, which is a numeric variable, into a String, Locals.VarString.
    I know that the Str() function can be used to convert a numeric into a String, but how can I store the actual words "Locals.MaxArrayIndex" in a string?
    Thanks! 

    Hmmm...it works great for Parameters that are arrays, but for me it won't work if the Parameter is a numeric variable. Maybe I'm making a mistake somewhere else that I'm not picking up; so I attached the sequence file if you want to take a look at it.
    The issue arises in the 'Else' statement. I'm pretty sure the syntax in my function and popup are correct, but the String (ParamName) that is supposed to hold the value of the Parameter variable name (DataSelect) doesn't seem to ever be filled and outputted in the error message.
    I would really appreciate it if any of you guys could have a look at the file (I'm sure I'm making some kind of really rookie mistake)
    Thanks! 
    Attachments:
    GenerateError.seq ‏9 KB

  • Substitute a variable name by String ?

    Hi
    is it possible to substitute a variable name by a String ? if yes then please tell me how :-)
    thx
    pain

    You might be able to use the Reflection API to get what you want, but every other time this question's been asked the asker really needed to use a HashMap instead...
    Good Luck
    Lee

  • Javascript variable names in strings

    This should be easy, but i can't find any info on how to do
    it!
    It's easy to put the contents of a variable into a string but
    how does one place the name of a Javascript variable into a
    string??
    The only solution i have found is to store the name
    separately as a string, but this isn't very neat.

    OK, simple statement first:
    Integer does not equal string.
    You also seem to be tossing around "runtime" and "static" somewhat liberally. Once you wire an integer (be it a control or block diagram constant) to the case structure's selector, the cases can only take on integer values. This is by design. If you wire a string, the case items take on string values. If you wire an enum, the case items take on the enumeration values. Once you've defined a case structure's cases you can't change them at "runtime". This, by definition, is different code.
    That said, it seems to me that you're looking for an enum.

  • What's user variable name using Smart View POV function (ex:HypGetActiveMember)?

    Hi,
    We want to use Smart View POV function (ex:HypGetActiveMember) to get the active member of user variables from HFM forms selected into Excel, but we don't know the user variable name in the following paragraph of Smart View developer guide:
    "To uniquely identify the user variable, provide the user variable name rather than the dimension name."
    For example, what's the user variable name of Year dimension? How to get the user variable name?
    Any information would be appreciated. Thank you!

    Hi,
    I guess user variable name is set by users/developers. I am not very clear on your question. If you have defined some variables to select any members from a dimension then it will be in HFM only. In planning we have two kinds of variables User Variables (we set it from Planning Preferences) and Substitution variable (we set it from Essbase Consol in the backend). Without looking into your application it is difficult to say the user variable name. Like for Year the user variables can be "PreviousYear", "CurrentYear", or "NextYear" etc.
    Soumya

  • Is there anyway i can identify a variable by it's name, using strings?

    I have the following:
    function ShowSlide(event:TweenEvent) {
    m = getChildByName("tag"+slideIndex+"_mc");
    m.visible = false;
    if (slideIndex < 4) {
    slideIndex++;
    }else{
    slideIndex = 1;
    m = getChildByName("tag"+slideIndex+"_mc");
    tagInitW = getChildByName("tag"+slideIndex+"InitW");
    tagInitH = getChildByName("tag"+slideIndex+"InitH");
    m.visible = true;
    new Tween(m, "width", Strong.easeOut, 1, tagInitW, 1, true);
    var tagTween:Tween = new Tween(m, "height", Strong.easeOut, 1, tagInitH, 1, true);
    timer = new Timer(2000, 1);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE, HideSlide);
    timer.start();
    And i want to do for "tagInitW" the same thing I do for "m" being given that i already have "created tag1InitW", "tag1InitH", "tag2InitW", "tag2InitH..."
    So is there anyway i can dinamically target a variable? This was easy in AS2, but because of the strict dataType the vars have in AS3, i can't do it like I was doing it in AS2.
    Loads of thanks.

    No.
    What I did with getChildByName was a stupid thing.
    tagInitW is a uint type variable and so is tagInitH. I also created tag1InitW, tag1InitH... of dataType uint which store the tag1_mc.width and tag1_mc.height for future use.
    What i am trying to do is to have tagInitW store the initial width of the coresponding movieClip. Because I am scaling the movieClips to 1px in the first place I can't make reference to tag1_mc.width later, so i store the width in tag1InitW before i scale them;
    So when that tween does it's tricks i want to have tagInitW = tag1InitW, tag2InitW etc, depeding on the sliderIndex which I increment repeatedly.
    Thanks a lot.

  • How to access variables indirectly using JSTL

    Hi,
    I have a variable called titleInsert pointing to a session variable someSessionVariable. The jsp has access to titleInsert but would like to output the value of someSessionVariable which is pointed to by tilesInsert.
    If I have something like
    <c:out value="${titleInsert}"/>
    the name of the session variable is being displayed but not the value.
    I tried something like
    <c:out value="$${titleInsert}"/> and
    <c:out value="${${titleInsert}}"/> and both resulted in JSP exceptions.
    Is there a way to display the value of someSessionVariable?
    Help appreicated,
    Kumar

    You use the implicit variable sessionScope, and the square brackets notation.
    <c:out value="${sessionScope[titleInsert]}"/>Cheers,
    evnafets

  • Creating variable names using a counter

    Hi all
    I have a snippet of code that I am trying to work in Flash 6,
    it is attached to this post. Inside the for loop I am trying to
    define the class variable instance names dynamically using the 'i'
    counter encapsulated within the eval function. Is there anyway I
    can add dynamically named variables or am I just kidding myself.
    regards
    John Walsh
    www.javono.com

    I think KGlad meant to post the following, probably
    accidentally did italics in bbcode
    import ButtonClass;
    var a:Array= new Array("alonso_btn", "gerrard_btn");
    for (var i = 0; i < a.length; i++) {
    this[a[ i ] = new ButtonClass();
    }

  • Finding a variable name using BCEL

    Let's say I have a simple class with the main method.
    The first line in the main method looks like this:
    boolean x = false;
    How would I find the NAME of that boolean ("x") using BCEL?
    Thank you very much in advance. I tried searching the internet, but there is very little documentation on BCEL.

    Well, first you need to read the VM spec to learn that local variables are stored as an attribute of a method: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#5956
    Then look at the BCEL class diagram, and note that JavaClass contains Methods, which contain Attributes, which may be a LocalVariableTable: http://jakarta.apache.org/bcel/images/javaclass.gif
    Then, it's time to go to the BCEL JavaDoc (which I agree, is difficult to find -- I didn't see a link to it, but I do know the structure of Apache projects): http://jakarta.apache.org/bcel/apidocs/
    At which point you'll see that the Method object has a method to retrieve the local variable table: http://jakarta.apache.org/bcel/apidocs/org/apache/bcel/classfile/Method.html#getLocalVariableTable()
    And, if you work your way through that, you'll see that (like everything else in the .class file), each local variable is a reference into the constant pool.
    Of course, to see local variables at all, you'll have to compile your original class with -g

  • Stored Procedure - variable name inside string [EXEC]

    Hi everyone
    I'm looking for a solution for this problem:
    I've created a stored procedure, in which i need to insert a record in a table.
    The point is i don't know the exact column to put my values and which values to save: it depends from the input data.
    I've created a function wich creates (concats) my query.
    The result is similar to:
    SET @myquery = 'INSERT INTO MyTable (columnABC, columnDEF) VALUES (@valueABC, @valueDEF)'
    This query is the value of a varchar.
    I want to execute this query, so I thought to use: EXEC(@myquery)
    The problem is that SQL can't find the variables @valueABC or @valueDEF if thery're inside a nvarchar.
    Is there some particular syntax in order to replace the value of variables?
    I hope it's clear!!
    Thank you in advance!!!!!
    Federica

    You need to create an entire SQL string without the variables.
    SET @myquery = 'INSERT INTO MyTable (columnABC, columnDEF) VALUES (''' + @valueABC + ''', ''' + @valueDEF + ''')'
    Please, please don't post such bad solutions!
    If you use dynamic SQL, you should use a parameterised statement:
    SET @myquery = 'INSERT INTO MyTable (columnABC, columnDEF) VALUES (@valueABC, @valueDEF)'
    EXEC sp_executesql @myquery, N'@valueABC int, @valueDEF int',
         @valueABC, @valueDEF
    However, I would prefer not to use dynamic SQL at all, but do:
    INSERT tbl(keycol, col1, col2, col3, ....)
       VALUES (@keyval,
               CASE @coltoinsert WHEN 'col1' THEN @val END,
               CASE @coltoinsert WHEN 'col2' THEN @val END,
    Although the entire idea of not knowing the columns before sounds spooky to me. A column in a table is supposed to model a unique attribute.
    Erland Sommarskog, SQL Server MVP, [email protected]
    sp_Executesql worked for me, than you very much!!!

  • Bizarre Question - Accessing Variables w/ a String

    Im obsessed with finding a way to do this, haha, but i dont know
    if its remotely possible.
    Instead of explaining it i will post some pseudo-code...
    class HackClass{
    int AVar = 1;
    int BVar = 2;
    int CVar = 3;
    // ...use some arbitrary method of getting user input
    String input = // user's input, in this case it is = "AVar"
    // and then what im trying to achieve...
    // im looking for something like this ---> (variable)input = 5;
    System.out.println("Avar = " + AVar); // this would output 5
    Note, this is very oversimplified. So though it would be easy in this example, i am not looking to register every
    variable in some table and then use some elaborate lookup function with the string, rather, is there a way to refer to a variable in code with a string?
    I cant imagine there is, but that would be WICKED.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Wow. Thanks for the code.
    Im writing test code now.
    You said this was "java-beanish"... is it reflection?
    Because the only way it compiles is if i import...
    import java.lang.reflect.*;also, in your code you never defined the variable 'object' in
    method.invoke(object, invokeParam);and for anyone else interested in the code - here is my test class:
    import java.lang.reflect.*;
    public class HackThis{
    public static void main(String[] args) {
         String var = "Var1";
         Integer value = new Integer( 1234 );
         HackThis ht = new HackThis();
         Class[] param = new Class[1];
         param[0] = value.getClass();
         Method method = ht.getClass().getMethod("set" + var, param);
         Object[] invokeParam = new Object[1];
         invokeParam[0] = value;
         method.invoke(object, invokeParam);
    public HackThis(){
    public Integer getVar1(){
         return Var1;
    public void setVar1(Integer i){
         Var1 = i;
         Integer Var1 = new Integer(99);
    }...but note it wont compile because 'object' hasnt been defined yet

  • How do I create a variable name on the fly?

    Hi,
    If I have a String containing a value, how do I create a variable name using that value during program execution? That is if I have
    String fred = "newvar";
    How do I then create a new String with the name "newvar" (using the contents of fred)? I will never know what the content of fred is until this point. What I'd like to be able to do is something like
    String fred.subString(0) = "a new value";
    Obviously this won't work but if it did the statement would evaluate to something like
    String newvar = "a new value";
    Appreciate any help you can give,
    Dave.

    Here u go:
       private JCheckBox AddCheckBox( String strText, int nTextID )
          JCheckBox checkbox = null;
          Class checkboxDefinition;
          Class[] stringArgsClass = new Class[] {String.class};
          Constructor stringArgsConstructor;
          Object[] stringArgs = null;
          String arg = "";
          arg = "CheckBox" + nTextID;
          String label = new String(arg);
          stringArgs = new Object[] {label};
          try
            checkboxDefinition = Class.forName("javax.swing.JCheckBox");
            stringArgsConstructor = checkboxDefinition.getConstructor(stringArgsClass);
            checkbox = (JCheckBox) createObject(stringArgsConstructor, stringArgs);
            checkbox.setName(label);
            checkbox.setText(strText);
            checkbox.setSize(new java.awt.Dimension(CQuestionBase.PAGEWIDTH, 21));
            checkbox.putClientProperty("1",new Integer(nTextID));
            checkbox.setVisible(true);
          catch (ClassNotFoundException e)
            System.out.println(e);
          catch (NoSuchMethodException e)
            System.out.println(e);
          return checkbox;
       }This code was based on an example I found about two years ago. You can use the same methodolgy to create objects of any kind. Again, look at java.lang.Reflect.

  • Renaming instance variable VS use of "this."

    Open Topic:
    Do you prefer this code?
    private var _myVar:int;
    public function myClassConstructor(value:int) {
         _myVar = value;
    or this code? (JAVA style)
    private var myVar:int;
    public function myClassConstructor(myVar:int) {
         this.myVar = myVar;
    Discuss the pros and cons, especially if performance is involved.
    You have 10 minutes... I will be collecting the papers

    Bertrand G. wrote:
    So no one is concerned about any performance issues using 'this'... I have seen somewhere that using 'this' in C++ is not as efficient as renaming the variable, because it makes an object instantiation or something like that.
    Most people will go for clarity of purpose in the code in preference to performance, simply because performance is not a critical factor for MOST purposes.
    I personally prefer using this for the setters and getters, this way I can have the same variable name for both the method parameters and the class variable.
    Just my 2 cents...
    I think the usual practice for getters and setters is to have a private variable for the instance variable and to name it with an underscore prefix to indicate that the variable is private. In that way references to the private variable are distinct to the variable name used in the setter and getter. In any event, in my setter I invariably name the parameter with the new value "newValue" or "newWhatever", some people just use "pWhatever" to indicate that variables have been passed as parameters, so the use of "this" is not required.
    I don't see the use of "this" as particularly relevant to performance, or getters and setters, and I would recommend naming private variables with an underscore prefix to indicate the use of a private instance variable, though not everyone follows this convention and may use other conventions, or indeed no convention at all.
    Paul

  • How to use string Variables for data provider names

    Really what I need to be able to do is insert the variable
    name in this:
    dataProvider="{chartXML2.lastResult.month.day}"
    I need to replace the "month" with a string variable name
    (i.e public var monthName = "January";)
    I also need to do the same for the categoryfield as it will
    not let me do categoryfield=$String.
    In PHP this is incredibly easy to do. You just say
    blah=$string . In fact, in PHP you can embed variable names
    everywhere. Its very powerful. For instance, in php, for the
    dataprovider issue above, i'd just do
    dataProvider="{chartXML2.lastResult.$StringName.day}"
    and it'd work just fine. Whats the syntax for doing this in
    Flex?

    You can use bracket notation. But don't. Use a reuslt handler
    to wrap the xmllist in an XMLListCollection instance variable and
    bind to that.
    Tracy

  • Create object name using a String variable

    Hi,
    I�m making a jsp page and I need to add several objects to a vector. The vector's name is arrayGrupo, and the object is Grupo. The problem is that when I create the instance of the object, I cant �give him different names, so the add in the Vector will always be of the same object instance. Is there anyway to create an object name using a variable?
    Something like this:
    <% if (request.getParameter("grupoNome") != null ) {
                                       nomeG=request.getParameter("grupoNome");
                                       descG=request.getParameter("grupoDesc");
                                       int nG=arrayGrupo.getTamanho(); //size of the array
                                       String xpto=String.valueOf(nG);
                                       Grupo "xpto"= new Grupo(); //here is the problem
                                       xpto.setNomeGrupo(nomeG);
                                       xpto.setDescricao(descG);
                                       xpto.setIdgrupo(arrayGrupo.getTamanho()+1);
                                       arrayGrupo.addGrupo(xpto);
    }%>
    Thanks
    Rui Gon�alves

    I want to show a list of objects that are stored in a vector. But I also want to add objects to that vector. If I just want to add one object, I can use this code:
    <% if (request.getParameter("grupoNome") != null ) {
                                       nomeG=request.getParameter("grupoNome");
                                       descG=request.getParameter("grupoDesc");
                                       Grupo newGrupo = new Grupo(nomeG,descG,arrayGrupo.getTamanho()+1);
                                       arrayGrupo.addGrupo(newGrupo);
    %>
    What should I do if I want to add more than one object to the vector?
    I don�t know if I made myself clear but is quite hard to explain my problem.
    Thanks

Maybe you are looking for