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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint