Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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!
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic