• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a String variable as an object name in a loop...Possible??

 
Marc Holbach
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi is it possible to create objects when the object name is a variable? My code looks like this right now:



The program up to this gets a size between 3 and 10 and I want to create the size^2 number of boxes...so if the user enters 10 I want to create 100 box objects with names box00 - box 99. However I am currently getting the error message that :
strBoxName is already defined in createGrid(java.awt.Graphics)

any help at will be appreciated...thanks in advance!!
 
Alexis Michael
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't to that

box strBoxName = new box(xPos,yPos,boxName,false);


This will create a variable named strBoxName, which will have nothing to do with the string strBoxName. So, when your loop gets to this line for the second time, you get the error since strBoxName is already defined by the first iteration of the loop.

May I suggest, use a 2D array of box objects and each time, create a new box at [verCounter][horCounter].

cheers
 
Marc Holbach
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot gonna try that right now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic