{"id":26,"date":"2008-07-21T12:08:50","date_gmt":"2008-07-21T19:08:50","guid":{"rendered":"http:\/\/www.nayansavla.com\/blog\/?p=26"},"modified":"2010-01-12T17:57:47","modified_gmt":"2010-01-13T00:57:47","slug":"references-and-how-they-work-in-actionscript","status":"publish","type":"post","link":"https:\/\/www.nayansavla.com\/blog\/?p=26","title":{"rendered":"References and how they work in Actionscript"},"content":{"rendered":"<p>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.\u00a0<\/p>\n<p>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,<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"actionscript codecolorer\"><span class=\"kw2\">function<\/span> referenceTest<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<span class=\"kw3\">void<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw2\">var<\/span> mainArray:<span class=\"kw3\">Array<\/span> = <span class=\"kw2\">new<\/span> <span class=\"kw3\">Array<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;1&quot;<\/span>,<span class=\"st0\">&quot;2&quot;<\/span>,<span class=\"st0\">&quot;3&quot;<\/span>,<span class=\"st0\">&quot;4&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"kw2\">var<\/span> tempArray:<span class=\"kw3\">Array<\/span> = mainArray;<br \/>\n<br \/>\ntempArray.<span class=\"kw3\">pop<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span>mainArray.<span class=\"kw3\">length<\/span> + <span class=\"st0\">&quot; is the length &quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>\u00a0<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Lets take a look at the picture below, forgive my drawing skills but i think it serves the purpose,<\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nayansavla.com\/uploads\/pics\/referencetut\/pointexample.jpg\" alt=\"Pointer example\" width=\"600\" height=\"450\" \/><\/p>\n<p>var mainArray:Array = new Array(&#8220;1&#8243;,&#8221;2&#8243;,&#8221;3&#8243;,&#8221;4&#8221;);<br \/>\nthis line of code actually creates a reference which we have called mainArray. This reference points to an array class.<\/p>\n<p>\u00a0<\/p>\n<p>var tempArray:Array = mainArray;<br \/>\none would expect this to create a duplicate array but no, we are just creating a new reference to the<br \/>\nsame memory block, this reference is called tempArray.<\/p>\n<p>\u00a0<\/p>\n<p>tempArray.pop();<br \/>\nnow 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.<\/p>\n<p>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.<\/p>\n<p>\u00a0<\/p>\n<p>if we do something like this, var tempArray:Array = new Array(&#8220;1&#8243;,&#8221;2&#8243;,&#8221;3&#8243;,&#8221;4&#8221;);<br \/>\nthen we are actually creating a new block of code in memory and any changes to tempArray won&#8217;t affect mainArray.<\/p>\n<p>hope now your doubts have cleared.<\/p>\n<p><strong>Remember, In Actionscript all complex data types are passed by reference and not by value.<\/strong><\/p>\n<p>\u00a0<\/p>\n<p>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.<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"actionscript codecolorer\">package <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\npackage <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">public<\/span> <span class=\"kw2\">class<\/span> SampleClass <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">public<\/span> <span class=\"kw2\">var<\/span> value:<span class=\"kw3\">String<\/span>;<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">public<\/span> <span class=\"kw2\">function<\/span> SampleClass<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>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.<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><div class=\"actionscript codecolorer\"><span class=\"kw3\">public<\/span> <span class=\"kw2\">var<\/span> dataArray:<span class=\"kw3\">Array<\/span>;<br \/>\n<br \/>\n<span class=\"co1\">\/\/initialization function with an array holding the sampleclass instances<\/span><br \/>\n<span class=\"kw3\">private<\/span> <span class=\"kw2\">function<\/span> init<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<span class=\"kw3\">void<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; dataArray = <span class=\"kw2\">new<\/span> <span class=\"kw3\">Array<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i:<span class=\"kw3\">Number<\/span>= <span class=\"nu0\">0<\/span>;i<span class=\"sy0\">&amp;<\/span>lt; <span class=\"nu0\">6<\/span> ;i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> tempSample:SampleClass = <span class=\"kw2\">new<\/span> SampleClass<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; tempSample.<span class=\"me1\">value<\/span> = <span class=\"kw3\">String<\/span><span class=\"br0\">&#40;<\/span>i<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; dataArray.<span class=\"kw3\">push<\/span><span class=\"br0\">&#40;<\/span>tempSample<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/this function is called on button click<\/span><br \/>\n<span class=\"kw3\">private<\/span> <span class=\"kw2\">function<\/span> findSample<span class=\"br0\">&#40;<\/span>event:MouseEvent<span class=\"br0\">&#41;<\/span>:<span class=\"kw3\">void<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/call the getSample function <\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> tempClass:SampleClass = getSample<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/if we find a match then we will change its value<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>tempClass<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot; found something lets change value&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; tempClass.<span class=\"me1\">value<\/span> = <span class=\"st0\">&quot;found yay&quot;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i:<span class=\"kw3\">Number<\/span> = <span class=\"nu0\">0<\/span>; i <span class=\"sy0\">&amp;<\/span>lt; dataArray.<span class=\"kw3\">length<\/span> ;i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/what do you this this trace statement will output<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/will it show the modified value or will output 0 through 5<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span>dataArray<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span>.<span class=\"me1\">value<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"co1\">\/\/this function is called from the findSample<\/span><br \/>\n<span class=\"kw3\">private<\/span> <span class=\"kw2\">function<\/span> getSample<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:SampleClass<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> foundSample:SampleClass;<br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/get the string on which matching is to be performed<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> findSample:<span class=\"kw3\">String<\/span> = sampleText.<span class=\"kw3\">text<\/span>;<br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i:<span class=\"kw3\">Number<\/span> = <span class=\"nu0\">0<\/span>; i <span class=\"sy0\">&amp;<\/span>lt; dataArray.<span class=\"kw3\">length<\/span> ;i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>dataArray<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span>.<span class=\"me1\">value<\/span> == findSample<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundSample = dataArray<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">break<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> foundSample;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Once you get run this modify the getSample function as shown below and compare the results. It should give you a better idea.<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"actionscript codecolorer\"><span class=\"co1\">\/\/this function is called from the findSample<\/span><br \/>\n<span class=\"kw3\">private<\/span> <span class=\"kw2\">function<\/span> getSample<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:SampleClass<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> foundSample:SampleClass = <span class=\"kw2\">new<\/span> SampleClass<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/get the string on which matching is to be performed<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> findSample:<span class=\"kw3\">String<\/span> = sampleText.<span class=\"kw3\">text<\/span>;<br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i:<span class=\"kw3\">Number<\/span> = <span class=\"nu0\">0<\/span>; i <span class=\"sy0\">&amp;<\/span>lt; dataArray.<span class=\"kw3\">length<\/span> ;i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>dataArray<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span>.<span class=\"me1\">value<\/span> == findSample<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundSample.<span class=\"me1\">value<\/span> = dataArray<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span>.<span class=\"me1\">value<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">break<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> foundSample;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>\u00a0<\/p>\n<p>hope this article was useful, as usual questions, comments always welcome \ud83d\ude42\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.\u00a0 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&#40;&#41;:void &#123; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[18,35,34],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-actionscript","category-flex","tag-flash","tag-pointers","tag-references"],"_links":{"self":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":0,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}