• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot gonna try that right now!
 
reply
    Bookmark Topic Watch Topic
  • New Topic