2.2 Namespaces

only for RuBoard

2.2 Namespaces

All classes are members of some namespace. You can think of a namespace as a user -defined scopean organizational construct that allows you to group your classes in a meaningful way and uniquely identify your classes and their members in case of naming conflicts. The Hello class from Example 2-1 is a member of a namespace called Greeting denoted by the Namespace block surrounding its definition. Even if the namespace block were removed, the Hello class would still be considered a member of the root namespace , which is scoped to the executable.

2.2.1 Imports

Every time anything is compiled with VB, two class libraries mscorlib.dll and Microsoft.VisualBasic.dll are referenced implicitly. The latter contains classes that provide backward compatibility to earlier versions of Visual Basic, while the former contains portions of the System and several other namespaces. Notice the second line of code in Example 2-1:

 Imports System 

This line brings the System namespace into the scope of the current file, hello.vb . This is done for the benefit of the call to Console.WriteLine , which writes a message to the console window. Without the Imports directive, the Console class could be referred to only through its namespace, which means the call would look like this:

 Public Class Hello
    Public Sub Write(ByVal value As String)  System.Console.WriteLine("Hello, {0}!", value)  End Sub
End Class 

This particular situation is not too bad, but if the file contained several calls to Console.WriteLine , things could get ugly. As most of the .NET class library is contained within the System namespace, importing it is usually your best option.

Framework Organization

The System namespace is defined in mscorlib.dll and System.dll. Although mscorlib.dll is referenced by the compiler automatically, System.dll is not.

If you need to use a class from a namespace in System.dll (or any other portion of the .NET class library), you have to add a reference to it when you compile with the /r compiler option. For example:

 vbc /t:exe /r:system.dll code.vb 

Other major portions of .NET include:

  • ASP.NET, which is mostly contained in System.Web.dll

  • Windows Forms, which is mostly contained in System.Windows.Forms.dll and System.Windows.Drawing.dll

  • Data and XML, which are contained in System.Data.dll and System.XML.dll

only for RuBoard
only for RuBoard

2.3 Using a Component

Now that you have a component, you need a way to use it. Example 2-2 contains a listing for a simple client. Save it to a file named hello-client.vb . Let's see how everything fits together, and then you can compile it.

Example 2-2. "Hello, world" client
 Imports System
Imports Greeting
   
Public Class Application
   
  Public Shared Sub Main( )
    Dim hw As New Hello( )
    hw.Write("World")
    Console.ReadLine( )
  End Sub
   
End Class 

The Greeting namespace defined in Example 2-1 was imported. Without it, every class in the Greeting namespace would have to be referenced directly, as in the following code:

 Public Shared Sub Main( )  Dim hw As New Greeting.Hello( )  hw.Write("World")
End Sub 

All standalone executables require an entry point with this signature:

 Public Shared Sub Main( ) 

This is where everything begins, but as you can see, not much is going on. The only thing the client does is declare an instance of the Hello class (which is defined in the component) and call its Write method.

When compiled, Example 2-2 must explicitly reference hello.dll for everything to compile. This can be accomplished with the /r compiler option. Assuming that the DLL lives in the same directory as the client code, this executable can be compiled as follows :

 C:\>vbc /t:exe /r:hello.dll hello-client.vb 

This compilation produces an executable named hello-client.exe . You can change the name of the output file by using the /out compiler option:

 C:\>vbc /t:exe /r:hello.dll /out:hello.exe hello-client.vb 

When the executable runs, the following code is dumped to the console:

 Hello, World! 
only for RuBoard