Home

Namespaces

 

Introduction

A namespace is a section of code that is recognized with a name. A namespace allows a programmer or a group of programmers who work with others to create code with names that don't conflict with code of the other programmers or other groups.

As mentioned already, a namespace is a section of code. Therefore, it must be created in a file before being used.

Creating a Namespace

The formula to create a namespace is:

Namespace name
    
End Namespace

As you can see, the creation of a namespace starts with the Namespace keyword and ends with the End Namespace expression. On the right side of the starting Namespace keyword, enter a name. The name follows the rules we have used so far for names in the Visual Basic language. Here is an example:

Namespace Census

End Namespace

The section between the starting Namespace line and End Namespace is the body of the namespace. In this body, you can write code as you want. For example, you can create a class in it. Here is an example:

Namespace Geometry
    Public Class Rectangle

    End Class
End Namespace

Of course, in the body of the class, you can write the necessary code. Here is an example:

Namespace Geometry
    Public Class Rectangle
        Public Width As Double
        Public Height As Double

        Public Function Perimeter() As Double
            Return (Width + Height) * 2
        End Function

        Public Function Area() As Double
            Return Width * Height
        End Function
    End Class
End Namespace

Accessing Members of a Namespace

The variables, procedures, and classes created inside a namespace can be referred to as its members.

To access a member of a namespace, you can use the period operator. To do this, where you want to access the member, type the name of the namespace, followed by a period, followed by the desired member of the namespace. Here is an example:

Namespace Geometry
    Public Class Rectangle
        
    End Class
End Namespace

Module Exercise

    Public Function Main() As Integer
        Geometry.Rectangle

        Return 0
    End Function

End Module

In the same way, wherever you want to access a member of a namespace, you can simply qualify it. Once the member has been qualified, you can use it as we saw in previous lessons. Here are examples:

Namespace Geometry
    Public Class Rectangle
        Public Width As Double
        Public Height As Double

        Public Function Perimeter() As Double
            Return (Width + Height) * 2
        End Function

        Public Function Area() As Double
            Return Width * Height
        End Function
    End Class
End Namespace

Module Exercise

    Public Function Main() As Integer
        Dim Rect As Geometry.Rectangle
        Rect = New Geometry.Rectangle

        Rect.Width = 40.18
        Rect.Height = 28.46


        MsgBox("Rectangle Characteristics" & vbCrLf & _
               "Width:" & vbTab & vbTab & FormatNumber(Rect.Width) & vbCrLf & _
               "Height:" & vbTab & vbTab & FormatNumber(Rect.Height) & vbCrLf & _
               "Perimeter:" & vbTab & FormatNumber(Rect.Perimeter()) & vbCrLf & _
               "Area:" & vbTab & vbTab & FormatNumber(Rect.Area()))
        Return 0
    End Function

End Module

In the same way, you can create as many members as you want in the body of a namespace.

In the above code, we create our namespace in the same file that contains the Main() function. To better manage your code, you can create your namespaces in different files. Here is an example:

Namespace Geometry
    Public Class Rectangle
        Public Width As Double
        Public Height As Double

        Public Function Perimeter() As Double
            Return (Width + Height) * 2
        End Function

        Public Function Area() As Double
            Return Width * Height
        End Function
    End Class

    Public Class Triangle
        Public Base As Double
        Public Height As Double

        Public Function Area() As Double
            Return Base * Height / 2
        End Function
    End Class
End Namespace

In the same way, you can create as many namespaces in as many files as you want. A namespace can be created in more than one file.

Importing a Namespace

When accessing each member of a namespace, you can qualify its name wherever you want to use it. Here are examples that use the above namespace that was created in a separate file:

Module Exercise

    Public Function Main() As Integer
        Dim Rect As Geometry.Rectangle = New Geometry.Rectangle

        Rect.Width = 40.18
        Rect.Height = 28.46

        MsgBox("Rectangle Characteristics" & vbCrLf & _
               "Width:" & vbTab & vbTab & FormatNumber(Rect.Width) & vbCrLf & _
               "Height:" & vbTab & vbTab & FormatNumber(Rect.Height) & vbCrLf & _
               "Perimeter:" & vbTab & FormatNumber(Rect.Perimeter()) & vbCrLf & _
               "Area:" & vbTab & vbTab & FormatNumber(Rect.Area()))

        Dim Tri As Geometry.Triangle = New Geometry.Triangle

        Tri.Base = 36.09
        Tri.Height = 28.46

        MsgBox("Triangle Characteristics" & vbCrLf & _
               "Base:" & vbTab & vbTab & FormatNumber(Tri.Base) & vbCrLf & _
               "Height:" & vbTab & vbTab & FormatNumber(Tri.Height) & vbCrLf & _
               "Area:" & vbTab & vbTab & FormatNumber(Tri.Area()))
        Return 0
    End Function

End Module

Instead of qualifying each member, you can import the namespace in the file where you want to use it. To do this, in the top section of the file, type Imports followed by the name of the namespace.

 
 
 

 

Nesting a Namespace

You can create one namespace inside of another namespace. This is referred to as nesting the namespace. To nest a namespace, create its body in the body of an existing namespace. Here is an example:

Namespace Geometry    

    Namespace Quadrilateral
        
    End Namespace

End Namespace

Inside the new namespace, you can implement the behavior you want. For example, you can create a class. The parent namespace is still its own namespace and can contain any necessary thing. Here is an example:

Namespace Geometry
    Friend Class Triangle
        Public Base As Double
        Public Height As Double

        Public Function Area() As Double
            Return Base * Height / 2
        End Function
    End Class

    Namespace Quadrilateral
        Public Class Rectangle
            Public Width As Double
            Public Height As Double

            Public Function Perimeter() As Double
                Return (Width + Height) * 2
            End Function

            Public Function Area() As Double
                Return Width * Height
            End Function
        End Class
    End Namespace

End Namespace

To access a member of the parent namespace, you can qualify it as we saw previously. To access a member of the nested namespace outside the parent, type the name of the nesting namespace, followed by a period, followed by the name of the nested namespace, followed by a period, and followed by the member you want to access. Here are examples:

Namespace Geometry
    Friend Class Triangle
        Public Base As Double
        Public Height As Double

        Public Function Area() As Double
            Return Base * Height / 2
        End Function
    End Class

    Namespace Quadrilateral
        Public Class Rectangle
            Public Width As Double
            Public Height As Double

            Public Function Perimeter() As Double
                Return (Width + Height) * 2
            End Function

            Public Function Area() As Double
                Return Width * Height
            End Function
        End Class
    End Namespace

End Namespace

Module Exercise

    Public Function Main() As Integer
        Dim Tri As Geometry.Triangle = New Geometry.Triangle

        Tri.Base = 36.09
        Tri.Height = 28.46

        MsgBox("Triangle Characteristics" & vbCrLf & _
               "Base:" & vbTab & vbTab & FormatNumber(Tri.Base) & vbCrLf & _
               "Height:" & vbTab & vbTab & FormatNumber(Tri.Height) & vbCrLf & _
               "Area:" & vbTab & vbTab & FormatNumber(Tri.Area()))

        Dim Rect As Geometry.Quadrilateral.Rectangle

        Rect = New Geometry.Quadrilateral.Rectangle
        Rect.Width = 40.18
        Rect.Height = 28.46

        MsgBox("Rectangle Characteristics" & vbCrLf & _
               "Width:" & vbTab & vbTab & FormatNumber(Rect.Width) & vbCrLf & _
               "Height:" & vbTab & vbTab & FormatNumber(Rect.Height) & vbCrLf & _
               "Perimeter:" & vbTab & FormatNumber(Rect.Perimeter()) & vbCrLf & _
               "Area:" & vbTab & vbTab & FormatNumber(Rect.Area()))

        Return 0
    End Function

End Module

In the same way:

  • You can create as many namespaces as you want inside of a nesting namespace
  • You can nest as many namespaces in as many nesting namespaces

Remember how to backwardly qualify the name of a member of a nested namespace. If the namespace exists in a separate file and you want to import it, type the Imports keyword, followed by the name of the parent namespace, followed by a period, and followed by the nested namespace. In the same way, you can import any nested namespace, as long as you appropriately qualify its name.

The System Namespace

To make programming in Visual Basic easier, many classes were created and stored in various namespaces. Each namespace is used to provide specific instructions.

One of the most regularly used namespaces in the Visual Basic language is called System. Inside of the System namespace is a class called Console. The Console class is used to display things on the console screen also called the DOS window.

The Console class contains procedures to display information on the screen or to retrieve information from the user who types it in the DOS window. The procedure that is used to display text on the screen is called Write. To use Write(), inside of the parentheses, type the sentence between double-quotes. Here is an example:

Module Exercise

    Public Function Main() As Integer

        System.Console.Write("Welcome")

        Return 0
    End Function

End Module

Besides Write(), the Console class also provides a procedure called WriteLine(). Here is an example:

Module Exercise

    Public Function Main() As Integer

        System.Console.WriteLine()

        Return 0
    End Function

End Module

The difference is that, after displaying something on the screen, the Write() method keeps the caret on the same line but WriteLine() transfers the caret to the next line.

We mentioned that, to access a member of a namespace, you could type the name of the namespace, followed by a period, and followed by the name of the member. This technique is referred to as qualifying a member of a namespace. This can be long ins some cases. Instead of qualifying a member of a namespace every time you want to use it, you can import the namespace into the file where you want to access its member(s). Here is an example:

Imports System

Module Exercise

    Public Function Main() As Integer

        Console.WriteLine()

        Return 0
    End Function

End Module
 
 
   
 

Home Copyright © 2009-2016, FunctionX, Inc.