May 23, 2002 - Using Qualified Class Names | WebReference

May 23, 2002 - Using Qualified Class Names

Yehuda Shiran May 23, 2002
Using Qualified Class Names
Tips: May 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

When you import several namespaces in your code, and you reference a class member (a property or method), there is always the question from which namespace this class is referenced. The compiler first searches the local scope. If it doesn't find the class, the compiler searches the classes in each of the imported namespaces, in the order they were imported, and stops when it finds a match.

You can control the namespace you want to reference by using a fully qualified name. Here is an example:

  import System.Collections;
  var my_queue : System.Collections.Queue;
Assuming several namespaces include different types of the Queue class, you can use the fully-qualified name to force usage of an explicit namespace, the System.Collections namespace. But if you know that the class Queue is defined only once in all imported namespaces, you can use a shorter non-qualified name:

  import System.Collections;
  var my_queue : Queue;
You can mix fully-qualified and non-qualified class names. The advantage of fully-qualified names is that the code is much easier to debug because you know exactly where the classes reside.

To learn more about JScript .NET, go to Column 109, JScript .NET, Part III: Classes and Namespaces.