Practical Examples of Namespaces in VB.NET 2005

This is the third article in a series focusing on inheritance in VB.NET 2005. In this article, I shall extend my previous article on namespaces to support it with a few more small and intelligent goodies.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 38
September 18, 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 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. 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.

Inheriting a class from another namespace

In my previous article, we examined the concept of namespaces in detail. Now, we shall develop classes by inheriting from other classes existing in other namespaces.

Let us examine the following namespace first:

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

From the above, you can understand that I defined a class "First" in the namespace "Namespace1."  Now, I would like to define another class, "Second," which must inherit the class "First" available in "Namespace1."

The next section shows you how to achieve this.

Inheriting a class from another namespace: continued

This is a continuation from the previous section.  Let us walk through the code first:

NamespaceNamespace2
    Public Class Second
        Inherits Namespace1.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

From the code above, you can understand that I defined a class "Second" in the namespace "Namespace2."  The most important declaration to concentrate on is the following:

    Public Class Second
        Inherits Namespace1.First

You can observe that the class "Second" (available in "Namespace2") gets inherited from the class "First" available in "Namespace1." If you forgot to specify "Namespace1" in the above scenario, it would have raised an error, because it could never find a class "First" which doesn't belong to any namespace.

To execute the above code, I need to modify the form 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 Namespace2.Second(10, 20, 30)
        Me.Label1.Text = obj.getSum
    End Sub
EndClass

Inheriting a class from a root namespace (or assembly)

This is a bit different from what we examined up to now. If I define a class without specifying any namespace, it generally belongs to a root namespace, which is generally the same as the assembly name itself (or the project name).

To put it simply, if you define classes which do not belong to any namespace, they straight away belong to assembly. It is quite easy to inherit from these types of classes, as we don't need to specify any name for the namespace during inheriting.

But there exists a small trick here to learn. We need to understand the "scope" of access. Even though I will not talk about "scope" in a detailed manner, I shall introduce you to a bit of it. In my upcoming articles, I shall explain in detail.

Every project you create will turn into an assembly at run time. That means, when you hit F5 (or run) to execute the project, it converts all of your code in all of your files into a single file called assembly. The physical and logical name of this assembly would be generally the same name as that of our project name (unless we configure it in a different way). To put it simply, an assembly is physical and a namespace is logical (but both will have the same names). An assembly physically stays in the file system (or at references), but we use namespaces (in our programming) to import/access the content of assembly.

To access any class/namespace within an assembly, we need to start with the name of the assembly of import first. All our classes and namespaces are encapsulated within this assembly (logically accessed with the root namespace name). If we wanted to inherit a class from a class within the same assembly and within the same root namespace, generally we don't need to specify the assembly name (or root namespace name). This is what we did (unknowingly) in the first few sections of my previous article.

The next section will give you some practical understanding of the above theory.

Inheriting a class from a root namespace (or assembly) in practice

This section proceeds based on the theory explained in the previous section.  Let us start from the beginning again. In this scenario, the name of the project (or assembly) is "SampleInheritance." Let us define a class (without any namespace) as follows:

PublicClass First
      .
      .
      .
EndClass

In the case above, without your understanding, it would be treated as something like the following (internally):

PublicClass SampleInheritance.First
      .
      .
      .
EndClass

Now, if you wanted to define a new class by inheriting from the above, you can define it in any of the ways as follows:

PublicClass Second
    Inherits First
      .
      .
      .
EndClass
PublicClass Second
    Inherits SampleInheritance.First
      .
      .
      .
EndClass

This type of usage or naming would be essential when you have same class names in root namespace and in your own created namespace. To make it simple, if you want to inherit from a class which doesn't belong to any namespace, kindly include the name of the assembly (or project name) to eliminate confusion and to attain better readability.

Finally, if you defined your own namespace as follows:

NamespaceNamespace2
      .
      .
      .
EndNamespace

it would also be treated something like the following (internally):

NamespaceSampleInheritance.Namespace2
      .
      .
      .
EndNamespace

Inheriting a class from a root namespace (or assembly): example

This section is based on the explanation in the previous two sections. Let us create a solution with the name "SampleInheritance" and add a simple class "First" as follows:

PublicClass First
    Private m_x As Integer
    Private m_y As Integer
    Public Sub New(ByVal a As Integer, ByVal b As Integer)
        m_x = a
        m_y = b
    End Sub
    Public Property X() As Integer
        Get
            Return m_x
        End Get
        Set(ByVal value As Integer)
            m_x = value
        End Set
    End Property
    Public Property Y() As Integer
        Get
            Return m_y
        End Get
        Set(ByVal value As Integer)
            m_y = value
        End Set
    End Property
EndClass

Please be aware that the above doesn't belong to any namespace; instead, it belongs to the root namespace (or the assembly). Let us add one more class "First" 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 above is another class with the same name "First" with a different set of properties. Observe that it is encapsulated within a new namespace, "Namespace1." The next section will show you how to inherit each of them according to your needs.

Inheriting a class from root namespace (or assembly): example continued

From the previous section, we have two classes with the same name "First" (but in different namespaces). Let us create one more class, "Second," 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 section is the trickiest part. The class "Second" gets inherited from the class "First."  Which "First"? Is it related to the root namespace or the namespace "Namespace1"?

First of all, the class "Second" belongs to the namespace "Namespace1." When this class gets inherited from another class, it checks whether that class exists within its own parent scope or not (in this case, it would be "Namespace1"). If it exists, it inherits from the same. This is called scope. So the above class "Second" gets inherited from the class "First" available in the namespace "Namespace1."

If we need to inherit the class "Second" from the class "First" of the root namespace, we may need to modify the code as follows:

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

In this article, I simply wanted to explain the topics of OOPS. The sample codes 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 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials