6/09/2011

Storing data in Arrays, Objects, Dictionaries

Flash provides several "quick" ways of storing data. Here's how to use Arrays, Objects, and Dictionaries for the purpose.


The (Sample) Problem
Let's say that we have a bunch of input text fields. We want each to have a default text that clears when a user clicks on it for input. It should also revert back to the default text when it loses focus and the field is empty. To do this, we need to associate each text field with its default text. Without creating custom classes to hold their default text, we can use Array, Object, or Dictionary to hold our information. Here's how:

Array
An Array takes positive integers to access its contents. Because of this, each text field will also need to be associated with a number. A quick way to do this is to have it in the text field's name. For example in the following code, the text fields on stage are named tf0,tf1,tf2,tf3,tf4,tf5 and so on. Also each text field already has their default texts inside. import flash.events.FocusEvent; var defaulttexts:Array=new Array(); // 6 because there are 6 text fields named from tf0-tf5 for(var i:int=0;i<6;i++){ defaulttexts.push(this["tf"+i].text); // save default text to array this["tf"+i].addEventListener(FocusEvent.FOCUS_IN,inFocus); this["tf"+i].addEventListener(FocusEvent.FOCUS_OUT,outFocus); } function inFocus(e:FocusEvent):void{ // get number by looking at name, // using substr to skip first two characters - "tf" var n:int=int(e.currentTarget.name.substr(2)); var dt:String=defaulttexts[n]; if(e.currentTarget.text==dt){ e.currentTarget.text=""; } } function outFocus(e:FocusEvent):void{ if(e.currentTarget.text==""){ var n:int=int(e.currentTarget.name.substr(2)); var dt:String=defaulttexts[n]; e.currentTarget.text=dt; } } As you can see, an extra association and a clever hack is needed to read data from an Array.

Object
An object uses a String to access its data instead of an integer. This makes things easier as we can just use each textfield's name. import flash.events.FocusEvent; var defaulttexts:Object=new Object(); for(var i:int=0;i<6;i++){ // associating textfield name with its default text defaulttexts[String("tf"+i)]=this["tf"+i].text; this["tf"+i].addEventListener(FocusEvent.FOCUS_IN,inFocus); this["tf"+i].addEventListener(FocusEvent.FOCUS_OUT,outFocus); } function inFocus(e:FocusEvent):void{ // just need to pass in textfield name to get associated default text var dt:String=defaulttexts[e.currentTarget.name]; if(e.currentTarget.text==dt){ e.currentTarget.text=""; } } function outFocus(e:FocusEvent):void{ if(e.currentTarget.text==""){ var dt:String=defaulttexts[e.currentTarget.name]; e.currentTarget.text=dt; } }
Dictionary
A Dictionary is much like the previous two except it takes any Object. So we can use the text field itself instead of using it's name or a number. import flash.events.FocusEvent; import flash.utils.Dictionary; var defaulttexts:Dictionary=new Dictionary(); for(var i:int=0;i<6;i++){ // using the text field itself to associate it to its default text defaulttexts[this["tf"+i]]=this["tf"+i].text; this["tf"+i].addEventListener(FocusEvent.FOCUS_IN,inFocus); this["tf"+i].addEventListener(FocusEvent.FOCUS_OUT,outFocus); } function inFocus(e:FocusEvent):void{ // just use e.currentTarget var dt:String=defaulttexts[e.currentTarget]; if(e.currentTarget.text==dt){ e.currentTarget.text=""; } } function outFocus(e:FocusEvent):void{ if(e.currentTarget.text==""){ var dt:String=defaulttexts[e.currentTarget]; e.currentTarget.text=dt; } }

2 comments:

  1. what is the functionality ????

    ReplyDelete
  2. what do you mean?
    This is just showing the ways you can store and access information.

    ReplyDelete