Skip to main content
Known Participant
January 18, 2013
Answered

problem with arrays

  • January 18, 2013
  • 1 reply
  • 2337 views

hi how are you guys ?  i hope you all fine ..

i have little problem with the arrays contents ..   

the problem is , when i try to trace the array items it shows the arrays items as objects like this

[object Object],[object Object],[object Object]

here is the source code (i lighted the trace message and the arrays )

package {

import flash.display.MovieClip;

   import flash.events.MouseEvent

   import flash.events.Event;

public class MAIN  extends MovieClip {

  var additemsvar:String

  var addpricevar:int

  var removeitemvar:int

  var itemsnp:Array = new Array (1000000)

 

  var za:int = 0

  public function MAIN () {

    addbutton.addEventListener(MouseEvent.CLICK ,addbutton1)

    removebutton.addEventListener(MouseEvent.CLICK ,removebutton1)

    addEventListener(Event.ENTER_FRAME , onEnterFrame)

  }

  public function onEnterFrame (event:Event){

   if(listbox1.selectedIndex != -1){

   itemname11.text = String({label:listbox1.getItemAt(listbox1.selectedIndex)})

  }

  }

 

  public function addbutton1 (event:MouseEvent){

  

   additemsvar = additems.text

   addpricevar = int (addprice.text)

  

   listbox1.addItem ({label:itemsnp[za]})

   listbox2.addItem ({label:itemsnp[za]})

   itemsnp.push({additemsvar:addpricevar});

   za += 1

   trace (itemsnp.toString())

  

  }

  public function removebutton1 (event:MouseEvent){

  

   trace (listbox1.selectedIndex)

  

   listbox1.removeItemAt(listbox1.selectedIndex)

  

  }

 

}

}

thank you

This topic has been closed for replies.
Correct answer kglad

but

itemsnp.push({additemsvar:addpricevar});

additemsvar and addpricevar are also strings variables

sorry because i asked you a lot but realy this thing confuse me

thank you


var additemsvar:String = "whatever";

1:  itemsnp.push({additemsvar:addpricevar});

itemsnp elements are objects, not primitives like strings or numbers.  trace(itemsnp) is expected to show [object Object] itemsnp.length number of times.

2.  itemsnp.push(additemsvar);

itemsnp elements are strings.  trace(itemsnp) is expected to show each string element (eg, "whatever").

1 reply

kglad
Community Expert
Community Expert
January 18, 2013

call traceItemsnpF to trace that:

function traceItemsnpF(a:Array):void{

for(var i:int=0;i<a.length;i++){

for(var s:string in a){

trace(s,a);

}

}

}

Known Participant
January 18, 2013

???

Inspiring
January 18, 2013

Looks like you don't have a problem with Arrays, but with objects!

The code kglad gave you should be used to trace your content so instead of

trace(itemsnp)

use

traceItemsnpF(itemsnp)