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