Practical Examples of Namespaces in VB.NET 2005 - Inheriting a class from another namespace: continued
(Page 2 of 6 )
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
Next: Inheriting a class from a root namespace (or assembly) >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee