SHARE:

Author: Posted on November 23, 2011 at 9:31 pm
Flex 3, Flex 3
SCRIPTS
0

Flex 3: Alert and outputting data from an Array, ArrayCollection or Object

For any of you who have moved from a language like JavaScript to Flex you will be missing that good old alert function which is often used for debugging so you know a function is running etc. If you are new to Flex it is not apparent right away that alert is available or how to access the Alert function.

This quick Flex tutorial will show you how to find alert and also some other little handy debugging methods.

First of all you are going to need to include the following lines to import some libraries:

import mx.utils.ObjectUtil;
import mx.controls.Alert;

Now you have these added to the top of your script file you will be able to use the following:

Alert.show(); //Use to alert a string to the screen
ObjectUtil.toString(); // Used to convert objects to a string

Ok, so how can alert and converting an object to a string be of any use? Well anyone who is a programmer will know that Alert can be used within a function to determine how far that function runs which can have many uses.

The more useful of the functions is the ability to convert an object to a string. An object in Flex can hold loads of data and it is often very useful to be able to see all of what the object is holding. An object also includes Array’s ArrayCollections and Object types. Use these functions in combination to output them to the screen:


[Bindable] private var arrCol:ArrayCollection = new ArrayCollection([
{name: “John Wilkinson”, number: “07856352711”},
{name: “Jane Summers”, number: “07785443622”},
{name: “Keith Doe”, number: “07778864567”},
{name: “Michael Johnson”, number: “0789993321”},
]); 

Alert.show(ObjectUtil.toString(arrCol)); 
/* OUTPUTS: 
(mx.collections::ArrayCollection)#0 
filterFunction = (null) 
length = 4 list = (mx.collections::ArrayList)#1 
length = 4 
source = (Array)#2 [0] 
(Object)#3 name = "John Wilkinson" number = "07856352711" [1] (Object)#4 name = "Jane Summers" number = "07785443622" [2] (Object)#5 name = "Keith Doe" number = "07778864567" [3] (Object)#6 name = "Michael Johnson" number = "0789993321" uid = "E4F515D1-B7A0-DF2B-600F-D254537F44D3" sort = (null) source = (Array)#2 
*/

As you can see this is very similar to PHP’s print_r() function that dumps the contents of an array! I hope this is of some use, please comment and provide any feedback!

  • /li>

  • REVIEWS

  • No categories have been added yet

TABLE OF CONTENTS