arrays issue: objects lose data
Monkey Forums/Monkey Programming/arrays issue: objects lose data| 
 | ||
| hello all, recently I restarted learning Monkey and I have a small issue: this is my code: 
For Local x:Int = 0 To numberOfHo - 1
     myHo[numberOfHo] = New cHO
     myHo[x].fName = "object " + x
Next 	           	           	
           	      
For Local x:Int = 0 To numberOfHo - 1										
     Print myHo[x].fName
     Print x
Next 
I assumed myHO[0] will have the name="object 0"; myHO[1] will have the name "object 1" and so on.. the problem is that all the myHO have name = "object 49" (numberOfHo = 50) at every loop all the objects change the name to the last one.. :| where is my mistake? | 
| 
 | ||
| numberOfHo is fixed -- you're declaring myHo [numberOfHo] over and over -- use myHo [x]. | 
| 
 | ||
| myHo[numberOfHo] = New cHO ''shouldbe myHo[x] = New cHO remember that "x" in your For-Next loop is the iterator value, whereas numberOfHo remains constant. | 
| 
 | ||
| yes there is a mistake, but the issue is still present: here it is the new code, i removed numberOfHo to be sure.. 
For Local x:Int = 0 To 10
	myHo[x] = New cHO
	myHo[x].fName = "obiect " + x
Next 	           	           	
           	      
For Local x:Int = 0 To 10									
     Print x
     Print myHo[x].fName				
Next  
BUT I STILL GET same name name for all the objects... | 
| 
 | ||
| I cant belive this. Here is the code: For Local x:Int = 0 To 10 myHo[x] = New cHO myHo[x].fName = "obiect " + x Print "New X: " + myHo[x].fName If x > 0 Print "Precedend X: " + myHo[x - 1].fName Next and here is the result:   | 
| 
 | ||
| Your code in the first of the two previous posts should work too. One issue I have sometimes seen is that when you are testing in HTML5 the browser sometimes reloads the old program if MServer is still running. Maybe that happened. If I fix a bug and the fix seems to do nothing, that's the first thing I check! | 
| 
 | ||
| tested with XNA, same result. also i tried this: 
   myHo[0] = New cHO()
   myHo[1] = New cHO()
           	
   myHo[0].fName = "nume 0"
   myHo[1].fName = "nume 1"
           	
   For Local x:Int = 0 To 1
        Print myHo[x].fName				
   Next 
prints me: nume 1 nume 1 :( | 
| 
 | ||
| How do you declare your array ? Something like global myHo:cHO[11] ? | 
| 
 | ||
| i am inside my Scene class: Field myHo:cHO[] = New cHO[100] | 
| 
 | ||
| i think you have fName  as Global when it should be a field. what does your class look like. | 
| 
 | ||
| yes! that was it.. now it is ok. noob me :| |