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.
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.).
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 PublicClass First Private m_a AsInteger Private m_b AsInteger PublicSubNew(ByVal x AsInteger, ByVal y AsInteger) m_a = x m_b = y EndSub PublicProperty A() AsInteger Get Return m_a EndGet Set(ByVal value AsInteger) m_a = value EndSet EndProperty PublicProperty B() AsInteger Get Return m_b EndGet Set(ByVal value AsInteger) m_b = value EndSet EndProperty EndClass EndNamespace
The next section will give you the explanation and sample code to activate the example shown above.
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 PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim obj AsNew Namespace1.First(10, 20) Me.Label1.Text = obj.A + obj.B EndSub EndClass
The most important statement from the above is only the following:
Dim obj AsNew 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 AsNew First(10, 20) Dim obj AsNew 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.”
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 PublicClass Second Inherits First Private m_c AsInteger PublicSubNew(ByVal x AsInteger, ByVal y AsInteger, ByVal z AsInteger) MyBase.New(x, y) m_c = z EndSub PublicProperty C() AsInteger Get Return m_c EndGet Set(ByVal value AsInteger) m_c = value EndSet EndProperty PublicFunction getSum() AsInteger Dim s AsInteger s = Me.A + Me.B + Me.C Return s EndFunction EndClass 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 PublicClass First . . . EndClass PublicClass Second Inherits First . . . EndClass EndNamespace
We need to modify the code in the form as follows to access the class “second” available in the namespace “Namespace1”:
PublicClass Form1 PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim obj AsNew Namespace1.Second(10, 20, 30) Me.Label1.Text = obj.getSum EndSub EndClass
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 PublicClass Third Inherits Second PublicSubNew() MyBase.New(0, 0, 0) EndSub PublicSubNew(ByVal a AsInteger, ByVal b AsInteger, ByVal c AsInteger) MyBase.New(10, 20, 30) EndSub PublicFunction getProduct() AsInteger Dim s AsInteger s = Me.A * Me.B * Me.C Return s EndFunction EndClass 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 PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim obj AsNew Namespace1.Namespace2.Third(10, 20, 30) Me.Label1.Text = obj.getProduct EndSub EndClass
Even though I presented the examples in a more understandable format, you are free to use any of the following methods.
NamespaceNamespace1 NamespaceNamespace2 PublicClass Third Inherits Second . . . EndClass 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 PublicClass First . . . EndClass PublicClass Second Inherits First . . . EndClass NamespaceNamespace2 PublicClass Third Inherits Second . . . EndClass 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.