Dynamic Arrays `?

Monkey Forums/Monkey Programming/Dynamic Arrays `?

Darky(Posted 2011) [#1]
<problem solved>
How i do this right ? i cannot resize my array for my menu class :(



Class Menu 
	Field Items:MenuItem[]
	Field Visible:Bool
	Field counter:Int
	Method New()
		Print "MENU CREATED"
	End
	
	Method Render()
		Local i:int
		For i = 0 to Items.Length -1
			DrawText (Items[i].caption,Items[i].x,Items[i].y)
		Next
	End
	Method AddItem(cid:int,xx:int,yy:int,capt:String)
		counter=counter+1
		Items=Items.Resize(counter)
		Items=[New MenuItem(cid,capt,xx,yy) ]
		Print "NEW ITEMCOUNT : "+Items.Length
		Print "CT:"+counter
	End
End
Class MenuItem 
	Field id:int
	Field y:int
	Field x:int
	Field caption:String
	
	Method New(cid:int,capt:String,cx:int,cy:Int)
		Print "MENU ITEM CREATED :" + capt + "("+cx+","+cy+")"
		id=id 
		y=cy
		x=cx
		caption=capt
		
	End
End


When i call AddItem a new item is created but the array with items allways will be 1 ... anyone have a solution for this problem please ? ...


greetz


anawiki(Posted 2011) [#2]
Shouldn't you type:

Items[counter-1] = New MenuItem(...)


Darky(Posted 2011) [#3]
I have solved this used lists :)



Import mojo
Class Menu 
	Field Items:List<MenuItem>
	Field Visible:Bool
	Field counter:Int
	Method New()
		Print "MENU CREATED"
		Items= new List<MenuItem>
	End


	Method Render()
		For Local d:MenuItem=eachin Items
			SetFont(
			DrawText (d.caption,d.x,d.y)
		Next
	End
	Method AddItem(cid:int,xx:int,yy:int,capt:String)
		Local nitem:MenuItem 
		nitem=New MenuItem(cid,capt,xx,yy)
		Items.AddLast(nitem)
	End
End
Class MenuItem 
	Field id:int
	Field y:int
	Field x:int
	public Field caption:String
	
	Method New(cid:int,capt:String,cx:int,cy:Int)
		Print "MENU ITEM CREATED :" + capt + "("+cx+","+cy+")"
		id=id 
		y=cy
		x=cx
		caption=capt
		
	End
End