Jul 21 2008

References and how they work in Actionscript

Published by at 12:08 pm under Flash,Flex

This tutorial/article is mainly for newbies/intermediate programmers. Someone who is writing Actionscript code without coming from C or Java background will benefit from this. 

A quick test to find if this article is for you, without executing this piece of code can you predict what will be the trace statement will output,

function referenceTest():void {
var mainArray:Array = new Array("1","2","3","4");
var tempArray:Array = mainArray;

tempArray.pop();

trace(mainArray.length + " is the length ");
}

 

Be honest and if you think the answer will be 4 then you definitely need to read this article and understand it. Still confused???? now you can run this piece of code and then you will find that the answer is 3.

how is this possible? We are not modifying mainArray at all, yes we are not modifying the reference to the array but then we are creating another reference to this array and then using it to modify the array. confused still, i need to explain it in more simple terms.

Lets take a look at the picture below, forgive my drawing skills but i think it serves the purpose,

 Pointer example

var mainArray:Array = new Array(“1″,”2″,”3″,”4”);
this line of code actually creates a reference which we have called mainArray. This reference points to an array class.

 

var tempArray:Array = mainArray;
one would expect this to create a duplicate array but no, we are just creating a new reference to the
same memory block, this reference is called tempArray.

 

tempArray.pop();
now we are using this reference called tempArray to modify the block of memory its pointing at, and hence that block of memory changes and we have only 3 elements in this array now.

Since mainArray is a reference pointing to the same block of memory as tempArray is, whenever we use mainArray reference it has changed now and its length is no longer 4.

 

if we do something like this, var tempArray:Array = new Array(“1″,”2″,”3″,”4”);
then we are actually creating a new block of code in memory and any changes to tempArray won’t affect mainArray.

hope now your doubts have cleared.

Remember, In Actionscript all complex data types are passed by reference and not by value.

 

Another test to find out if you understood how this works, try the following code, I am not including the graphical elements and i shall leave upto you to fill out the code for that.

package {

package {
    public class SampleClass {

        public var value:String;

        public function SampleClass() {

        }
    }
}

A sample test class to be used in our example, I am copying this code from Flex so will include only the actionscript code, you need to do proper initialization etc.

public var dataArray:Array;

//initialization function with an array holding the sampleclass instances
private function init():void{
    dataArray = new Array();
    for (var i:Number= 0;i< 6 ;i++){
        var tempSample:SampleClass = new SampleClass();
        tempSample.value = String(i);
        dataArray.push(tempSample);
    }
}

//this function is called on button click
private function findSample(event:MouseEvent):void{
   
    //call the getSample function
    var tempClass:SampleClass = getSample();

    //if we find a match then we will change its value
    if (tempClass){
        trace(" found something lets change value");
        tempClass.value = "found yay";
    }

    for (var i:Number = 0; i < dataArray.length ;i++){
        //what do you this this trace statement will output
        //will it show the modified value or will output 0 through 5
        trace(dataArray[i].value);
    }
}


//this function is called from the findSample
private function getSample():SampleClass{

    var foundSample:SampleClass;

    //get the string on which matching is to be performed
    var findSample:String = sampleText.text;

    for (var i:Number = 0; i < dataArray.length ;i++){
        if (dataArray[i].value == findSample){
            foundSample = dataArray[i];
            break;
        }
    }

    return foundSample;
}

Once you get run this modify the getSample function as shown below and compare the results. It should give you a better idea.

//this function is called from the findSample
private function getSample():SampleClass{

    var foundSample:SampleClass = new SampleClass();

    //get the string on which matching is to be performed
    var findSample:String = sampleText.text;

    for (var i:Number = 0; i < dataArray.length ;i++){
        if (dataArray[i].value == findSample){
            foundSample.value = dataArray[i].value;
            break;
        }
    }

    return foundSample;
}

 

hope this article was useful, as usual questions, comments always welcome :) 

 

No responses yet

Trackback URI | Comments RSS

Leave a Reply