WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

An Introduction to the .NET FCL, Part 3
Pages: 1, 2, 3, 4, 5

Accessing Types in Namespaces

Once you've added a reference to an assembly, you can access any of the types in its namespaces by providing a fully qualified reference to the type. For instance, the code in Example 1-4 instantiates objects of the HashTable and DictionaryEntry classes, and also calls a method of the Console class.

Example 1-4. Using fully qualified namespace names

Option Strict On
  
Public Module modMain
   Public Sub Main
      ' Define hashtable
      Dim States As New System.Collections.HashTable()
      ' Add items
      States.Add("NY", "New York")
      States.Add("CA", "California")
      States.Add("MI", "Michigan")
      States.Add("VT", "Vermont")
      States.Add("WA", "Washington")
 
     ' Define and fill DictionaryEntry object
      Dim dict(States.Count - 1) As _
               System.Collections.DictionaryEntry
      Dim item As System.Collections.DictionaryEntry
      States.CopyTo(dict, 0)
  
      ' Iterate dictionary
      For Each Item in dict
         System.Console.WriteLine( _
            Microsoft.VisualBasic.Strings.UCase(CStr( _
            item.Key)) & ": " & CStr(item.Value))
      Next
   End Sub
End Module

In each case, the source code includes the fully qualified name of the type it instantiates or accesses; the Console class is a type in the System namespace, while the HashTable and DictionaryEntry classes are both types in the System.Collections namespace. Note that even the namespace of the supposedly "intrinsic" Visual Basic UCase function must be specified or a compiler error ("Name 'UCase' is not declared.") results if you attempt to compile the program using the Visual Basic command-line compiler. The UCase function, as you can see from the code in Example 1-4, is a member of the Strings class in the Microsoft.VisualBasic namespace.



Importing Namespaces

Needless to say, fully qualifying the name of each .NET type quickly becomes rather tiresome, particularly for types that are nested deep within a hierarchical namespace. You can, however, use the Imports directive to import a particular namespace, thereby allowing the compiler to resolve the reference to a particular type and eliminating the need for you to provide a fully qualified path to the type. For example, the code fragment shown in Example 1-4, when rewritten to use the Imports directive, appears as shown in Example 1-5 (new and modified lines of code appear in boldface).

Example 1-5. Importing namespaces with the Imports directive

Option Strict On
  
Imports System
Imports System.Collections
Public Module modMain
  
Public Sub Main
   ' Define hashtable
   Dim States As New HashTable()   ' Add items
   States.Add("NY", "New York")
   States.Add("CA", "California")
   States.Add("MI", "Michigan")
   States.Add("VT", "Vermont")
   States.Add("WA", "Washington")
  
   ' Define and fill DictionaryEntry object
   Dim dict(States.Count - 1) As DictionaryEntry
   Dim item As DictionaryEntry   States.CopyTo(dict, 0)
  
   ' Iterate dictionary
   For Each Item in dict
      Console.WriteLine(CStr(item.Key) & ": " & _
                        CStr(item.Value))   Next
End Sub
  
End Module

Related Reading

VB.NET Core Classes in a Nutshell
By Budi Kurniawan, Ted Neward

Note that while no namespaces are automatically imported by the command line compiler, Visual Studio automatically imports a number of namespaces, again depending on the project type. The project types and the namespaces that they automatically import are as follows:

ASP.NET web applications

Microsoft.VisualBasic

System

System.Collections

System.Configuration

System.Data

System.Drawing

System.Web

System.Web.UI

System.Web.UI.HTMLControls

System.Web.UI.WebControls

Pages: 1, 2, 3, 4, 5

Next Pagearrow