Developing Namespaces in VB.NET 2005 - A namespace with more than one class
(Page 4 of 6 )
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
Next: A simple example for a nested namespace >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee