Hi All! First thanks for viewing my thread. I have spent a half day and change trying to find a solution to this specific problem.
Basically I am trying to figure out how to change the variable name while in a loop. The last loop is where my problem is with some comment code describing a little bit about whats going on. Thanks for the help in advance.

//Class Constructor method
public Display(Main inMain, int numGui) {
        System.out.println("Display(Main inMain)--from Display");
        //place gui's inside an arraylist
               guiArray.add(gui);
        guiArray.add(gui2);
        guiArray.add(gui3);
        guiArray.add(gui4);


        this.main = inMain;
        //find the number of guis to delete out of four possible
                int numDelete = -numGui + 4 ;

        System.out.println("Start Size" + guiArray.size());
        for(int j = 0; j<numDelete; j++){
            //delete the gui's we do not need
            guiArray.remove(0);
            }

        System.out.println("Stop Size" + guiArray.size());
        for(int n = 0; n< guiArray.size(); n++){

         guiArray.get(n);

                // guiArray.get(n) = new Gui(this);  **I need this line
        // I need this line to display the gui properly but it gives 
               //me the error   "the left hand side of an assignment 
               //must be a variable" so I might have to use if statements
               //but then the code is not scaleable to I am confused.
        }
        /*
       gui = new Gui(this);// gui
        gui2 = new Gui(this);
        gui3 = new Gui(this);
        gui4 = new Gui(this);
        */
    }

Recommended Answers

All 5 Replies

Basically I am trying to figure out how to change the variable name while in a loop.

Change the variable name of what? And why?

guiArray.get(n) = new Gui(this);

You can't use get this way. I'm not sure what you are trying to do, but you can do this:

Gui someGUI = guiArray.get (n);

or this:

Gui someGUI = new Gui (this);
guiArray.set (n, someGUI);

What are you trying to do?

I am working on a fairly good size open source application so to display all the code is not sensible. Basically my part is: the user will choose how many GUI's to display on a very large GUI. ( The constructor method will bring in the "int numGui" variable which is "the number of GUI's from 1-4 he wants to see" Then from the GUI arraylist which already contains the max 4 GUI's the code will remove however many that is needed so the amount that is left over matches the user defined "int numGui".
That may sound a bit complicated. I will display the previous constructor code that works but is not scalable. ((I'm simply trying to figure out a dynamic way to display a user defined number of GUIs from an already existing GUI class" Hope that answers some of your questions. Either way your input makes me think out of the box which is what I need now. Thanks Again!
Dan

DmoDisplay(DmoMain inMain) {
    System.out.println("DmoDisplay(DmoMain inMain)--from Display");

    this.main = inMain;

            //The below line is what I cannot replicate in my code
           //dynamically through a loop .
           //It is very possible there is a much simpler way of doing 
            //all of this
    gui = new DmoGui(this);


}

In my previous post if I knew of a way to increment the variable name inside that loop I think I would be fine.
I thought the below line would work

guiArray.get(n) = new Gui(this);

Since the below line basically equals

gui = new DmoGui(this);

Also just so I do not confuse you any more.

DmoGui(this);
Gui(this);

Are the same thing but variable name changed.

I am working on a fairly good size open source application so to display all the code is not sensible. Basically my part is: the user will choose how many GUI's to display on a very large GUI. ( The constructor method will bring in the "int numGui" variable which is "the number of GUI's from 1-4 he wants to see" Then from the GUI arraylist which already contains the max 4 GUI's the code will remove however many that is needed so the amount that is left over matches the user defined "int numGui".
That may sound a bit complicated. I will display the previous constructor code that works but is not scalable. ((I'm simply trying to figure out a dynamic way to display a user defined number of GUIs from an already existing GUI class"

This makes sense. I can visualize it.

In my previous post if I knew of a way to increment the variable name inside that loop I think I would be fine.
I thought the below line would work
guiArray.get(n) = new Gui(this);

Since the below line basically equals
gui = new DmoGui(this);

"Increment the variable name" - I'm having trouble visualizing what you mean by this term. How do you increment a name, variable or otherwise? Does the name "aaa" turn into "aab", as in "increment" it alphabetically or something?

Also just so I do not confuse you any more.

DmoGui(this);
Gui(this);

Are the same thing but variable name changed.

Thanks for trying not to confuse me more, but I think I am more confused. :confused: DmoGui and Gui aren't variable names above, they are class names.


Are you trying to accomplish this below?

ArrayList <Gui> GetGuisToDisplay (ArrayList<Gui> guis, int numGuis)

// assumes that guis contains four elements.  ArrayList returned should contain first numGuis elements of guis, which are the Guis to be displayed.  Assumes numGuis will be in the range of 0 to 4.

Misperception object reference & accessors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.