Lesson 5

internal

We need to also declare a variable or function as public when we call it from a different class. However, there is another word which we can use, which is "internal". If we call an internal function, which belongs to a class of one package as shown below, then there is no error and the variable value is displayed. However, if we call an internal variable from a different package, we will get this error:

                    ReferenceError: Error #1069: Property myVar not found on supplement.Suplemental_2 and there is no default value.
	at Starter_6/Starter_6::myTest()
	at Starter_6$iinit()

Therefore, internal is slightly different from private and is similar to private when we deal with different packages.

Here is the script.

package 
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import supplement.Suplemental_2;
	
	public class Starter_6 extends Sprite 
	{
	   private var aField:TextField;
	   private var bField:TextField;
	   public function Starter_6 () 
	   {
	       myTest();
	   }
	   private function myTest():void 
	   {
	               var a:Testvar = new Testvar();
	               aField = new TextField();
	               aField.autoSize = "left";
	               aField.background = true;
	               aField.border = true;
	               addChild(aField);
	       
	               var b:Suplemental_2 = new Suplemental_2 ();
	               bField = new TextField();
	               bField.autoSize = "left";
	               bField.background = true;
	               bField.border = true;
	               bField.y = 75;
	               bField.text = "Test2 is: "+b.myVar;
	               addChild(bField);
	       }
	}
}
import flash.display.Sprite;
class Testvar extends Sprite 
{
	private var test:String;
	public function Testvar():void 
	{
	}
	internal function get myVar():String 
	{
	   test = "I am private but if you see me I am public.";
	   return test;
	}
}

previous  next