May 27, 2002 - Using Qualified Names | WebReference

May 27, 2002 - Using Qualified Names

Yehuda Shiran May 27, 2002
Using Qualified Names
Tips: May 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

You are always on the safe side when using qualified names, especially when you import namespaces. Why be surprised later with a name collision and unexpected behavior of your code? The following example demonstrates the need for qualified names. There are three imported namespaces, defined as follows:

  package USA {
     class Head {
       static var President : String = "Bush";
     }
  };
  
  package UK {
     public class Head {
        static var PrimeMinister : String = "Blair";
     }
     public class Localization {
       static var Currency : String = "Pound";
     }
  };
  package USA.Florida {
    public class Head {
      static var Governor : String = "Bush";
    }
  };
Let's declare a local class now:

  class Head {
    static var Governor : String = "Davis";
  }
Let's import the three databases from above:

  import USA;
  import UK;
  import USA.Florida;
And then let's print some class members:

  print(Head.Governor);
  print(USA.Head.President);
  print(UK.Head.PrimeMinister);
  print(USA.Florida.Head.Governor);
  print(Localization.Currency);
  print(UK.Localization.Currency);
Here is the output of the above code:

  Davis
  Bush
  Blair
  Bush
  Pound
  Pound
Notice that when the class location is ambiguous, you must use fully-qualified names. The class Head, for example, appears in USA, UK, USA.Florida, and locally, so the namespace must prefix this class. The class Localization, however, appears only in the UK namespace, so there is no need to use the fully-qualified variable names.

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