Developing Namespaces in VB.NET 2005

This is the second article in a series focusing on inheritance in VB.NET 2005. In this article, I shall introduce you to developing namespaces in VB.NET 2005, together with inheritance.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
September 11, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable file for this article is available here.

If you are new to inheritance in Visual Basic.NET, I request that you go through my first article in this series titled “Inheritance with VB.NET 2005.”  If you are very new to OOP in VB.NET, I request that you go through my first article in the series “Object Oriented Database Development Using VB.NET.”

The entire source code for this article is available in the form of a downloadable zip file. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition. I didn’t really test the solution with any other previous editions. If you have any problems in executing the solution, please post in the discussion area.

What exactly is a namespace?

Everybody knows that the fundamental unit of OOPS is a “class.”  In my previous articles (as specified above), I declared, defined and explained several classes with several members in different ways. 

To develop an application, you are free to declare and define any number of classes. When you are dealing with too many classes, sometimes, it may be necessary for you to categorize them in a meaningful manner. Just like you encapsulate a set of members into a class, you may need to have a “categorization of classes.” 

A set of classes belonging to a particular category can be grouped into a “namespace.”  It is a logical structure, which contains only classes in it. Just as a class has its own name, a namespace should also be defined with its own name (or instead, the category name of the classes!).

Did we ever use namespaces before in our previous applications/samples?  Yes, but all of them were pre-defined namespaces available in the huge .NET Framework class library. The .NET Framework Class Library has a huge set of pre-defined “namespaces” with thousands of “classes” hierarchically designed. 

Let us consider the following statement:

System.Console.WriteLine("Hello World!");

According to the above statement, you are trying to execute a method named “WriteLine” (which is “shared”) in the class “Console” available in the namespace “System.”  When we use the statement “imports,” we are actually importing all the classes available within the respective “namespace.” Don’t forget that a “namespace” can also be nested with several other “sub-namespaces” (ex: System.Web.UI etc.).

A simple example of a namespace

In my previous article (“Inheritance in VB.NET 2005”), I declared three classes, namely “First,” “Second” and “Third.”  I shall use the same solution and further extend it with examples of “namespaces.”

The simplest construction of a namespace would be as follows:

NamespaceNamespace1
EndNamespace

Every namespace should start with the keyword “Namespace” followed by the name of the namespace (in this case, it is “Namespace1”).  It is always recommended to name the namespace in a meaningful manner to identify a group of classes. 

To the same solution, add a new class file named “Namespace1_First.vb” and modify the code as follows:

NamespaceNamespace1
    Public Class First
        Private m_a As Integer
        Private m_b As Integer
        Public Sub New(ByVal x As Integer, ByVal y As Integer)
            m_a = x
            m_b = y
        End Sub
        Public Property A() As Integer
            Get
                Return m_a
            End Get
            Set(ByVal value As Integer)
                m_a = value
            End Set
        End Property
        Public Property B() As Integer
            Get
                Return m_b
            End Get
            Set(ByVal value As Integer)
                m_b = value
            End Set
        End Property
    End Class
EndNamespace

The next section will give you the explanation and sample code to activate the  example shown above.

Accessing a class available in a namespace

This section explains the code provided in the previous section and further proceeds with the same.

From the previous section, you could observe that I included a new class, “First,” within the namespace “Namespace1.”  You should also observe that there exists another “First” class within the same solution.  But it doesn’t display any error because we embedded the newly added class into a “namespace.”  Try removing the namespace construct and you will see an error as the result!

To work with the above class, I modified the code as follows:

PublicClass Form1
    Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Namespace1.First(10, 20)
        Me.Label1.Text = obj.A + obj.B
    End Sub
EndClass

The most important statement from the above is only the following:

        Dim obj As New Namespace1.First(10, 20)

The above statement creates an object named “obj” based on the class “First” which exists in the namespace “Namespace1.”  That’s the trick to accessing a class available in a namespace.

For further clarity, let us understand the following two statements:

        Dim obj As New First(10, 20)
        Dim obj As New Namespace1.First(10, 20)

Both of the above statements look very similar.  At this point, we must be careful enough to understand the difference between the two.  In our solution, I have two classes with the same name, “First.” One is inside the namespace “Namespace1” and the other is outside. The first statement creates the object based on the class “First” which is declared outside “Namespace1.” The second statement creates the object based on the class “First” which is declared inside “Namespace1.”

A namespace with more than one class

In the previous section, I dealt with only one class in the namespace.  Now, let us add one more class file named “Namespace1_second.vb” and modify the code as follows:

NamespaceNamespace1
    Public Class Second
        Inherits First
        Private m_c As Integer
        Public Sub New(ByVal x As Integer, ByVal y As Integer,
ByVal z As Integer)
            MyBase.New(x, y)
            m_c = z
        End Sub
        Public Property C() As Integer
            Get
                Return m_c
            End Get
            Set(ByVal value As Integer)
                m_c = value
            End Set
        End Property
        Public Function getSum() As Integer
            Dim s As Integer
            s = Me.A + Me.B + Me.C
            Return s
        End Function
    End Class
EndNamespace

The above code contains only one class “Second” in the namespace “Namespace1.” But, from the point of view of our Visual Studio solution, we have two classes, “First” and “Second,” in the same namespace, “Namespace1.” The important point here to remember is that “classes in several files may also belong to the same namespace based on the declarations.” 

If you prefer to embed several classes in the same namespace within the same file, it is still possible with the following type of construct:

NamespaceNamespace1
    Public Class First
      .
      .
      .
    End Class
    Public Class Second
        Inherits First
      .
      .
      .
    End Class
EndNamespace

We need to modify the code in the form as follows to access the class “second” available in the namespace “Namespace1”:

PublicClass Form1
    Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Namespace1.Second(10, 20, 30)
        Me.Label1.Text = obj.getSum
    End Sub
EndClass

A simple example for a nested namespace

Up until now, we have examined only the outer level namespaces.  Now, let us work with nested namespaces.  A nested namespace (or sub-namespace) is a namespace which exists within another namespace. They can be nested any number of times.

Let us add one more class file named “Namespace2_third.vb” and modify the code as follows:

NamespaceNamespace1.Namespace2
    Public Class Third
        Inherits Second
        Public Sub New()
            MyBase.New(0, 0, 0)
        End Sub
        Public Sub New(ByVal a As Integer, ByVal b As Integer,
ByVal c As Integer)
            MyBase.New(10, 20, 30)
        End Sub
        Public Function getProduct() As Integer
            Dim s As Integer
            s = Me.A * Me.B * Me.C
            Return s
        End Function
    End Class
EndNamespace

The most important line to understand from the above code is the following line:

NamespaceNamespace1.Namespace2

It shows that we are defining one or more classes for the namespace “Namespace2” which resides in another namespace, “Namespace1.” To access the above class “Third,” I need to modify the code as follows:

PublicClass Form1
    Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Namespace1.Namespace2.Third(10, 20, 30)
        Me.Label1.Text = obj.getProduct
    End Sub
EndClass

Summary

Even though I presented the examples in a more understandable format, you are free to use any of the following methods.

NamespaceNamespace1
NamespaceNamespace2
            Public Class Third
                  Inherits Second
                  .
                  .
                  .
      End Class
EndNamespace
EndNamespace

In the above method, I specifically nested “namespace2” in “namespace1” just for the sake of a “nested look.”  There is no problem with using the above method.

For that matter, I can also declare and define the whole namespace with all of its nested namespaces and classes as follows:

NamespaceNamespace1
    Public Class First
      .
      .
      .
    End Class
    Public Class Second
        Inherits First
      .
      .
      .
    End Class
NamespaceNamespace2
            Public Class Third
                  Inherits Second
                  .
                  .
                  .
      End Class
EndNamespace
EndNamespace

The above is also valid, but rarely practiced, when we have hundreds of lines of code in each class.  It all depends on you to choose.

In this article, I simply wanted to explain the topics of OOPS. The code samples given in this article are neither the best in performance nor the best in programming methodologies.

Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials