Sunday, February 17, 2013

Referencing namespaces

There is no need to explicitly reference a namespace when you use a method or property declared with any of the access control namespaces, such as public, private, protected, and internal. This is because access to these special namespaces is controlled by context. For example, definitions placed into the private namespace are automatically available to code within the same class. For namespaces that you define, however, such context-sensitivity does not exist. In order to use a method or property that you have placed into a custom namespace, you must reference the namespace.

You can reference namespaces with the use namespace directive or you can qualify the name with the namespace using the name qualifier (::) punctuator. Referencing a namespace with use namespace “opens” the namespace, so that it can apply to any identifiers that are not qualified. For example, if you have defined the example1 namespace, you can access names in that namespace by using use namespace example1.

use namespace example1;
myFunction();

You can open more than one namespace at a time. Once you open a namespace with use namespace, it remains open throughout the block of code in which it was opened. There is no way to explicitly close a namespace.

Having more than one open namespace, however, increases the likelihood of name conflicts. If you prefer not to open a namespace, you can avoid the use namespace directive by qualifying the method or property name with the namespace and the name qualifier (::) punctuator. For example, the following code shows how you can qualify the name myFunction() with the example1 namespace.

example1::myFunction();
Programming Actionscript 3. Powered by Blogger.