Visual Studio.NET Namespaces
The .NET Framework class library has thousands of classes which are needed for developing and deploying solutions. In order to organize all those classes for ease of use .NET Framework uses namespaces. This Gives the Classes their own space and prevents conflicts between the various names in these classes. For instance if two classes contain a method Paint(), then to avoid conflicts in names we can place these classes in two different namespaces. Thus namespaces allow classes to be grouped in a consistent, hierarchical manner.
The writing convention is that the word after the right–most dot is the name of the type and the string up to the dot is the name of the namespace. We shall see an example of this in the following statement.
System.Windows.Forms.Button
In the above statement the name of the namespace is System.Windows.Form and the type name is Button.
A namespace can contain classes, structures, enumerations, delegates, interfaces, and other namespaces. Namespaces can be nested and can have any number of members. The typical namespaces begin with Microsoft or System. The new namespace My is added in .NET 2005. If there is a conflict in the namespace in such a way that even fully qualified Object name is also not usable, then the classes cannot be used.
You can create a namespace by using the Namespace … End Namespace block. With in the namespace Block, you can create classes, enumerations, structures, delegates, interfaces, or other namespaces. It is not imperative that all the code should be kept in one single file. A namespace can span multiple files and even multiple assemblies.
Namespace VBTutorial
Class Class1
…….
End Class
Namespace Lesson2
Class Class2
…..
Public Sub Teach()
End Sub
End Class
End namespace
End Namespace
In order to access the methods teach defined in class2 you have to instantiate the class Class2.
Dim lessonObj as new VBTutorial.Lesson2.Class2
lessonObj.Teach()
Alternatively you can use the following lines of code:
Imports VBTutorial.Lesson2
Dim lessonObj as new Class2
lessonObj.Teach()
Let us quickly see some of the namespaces defined in .NET
System.ComponentModel |
Liending and design time implementation of components |
System.Data |
Data Access |
System.Data.SQLClient |
SQL Server data access |
System.Data.OLEDB |
OLE DB data access |
System.Data.SML |
XML processing |
System.Diagnostics |
Provides debugging and tracing services |
System.Messaging |
Microsoft Message Queue management |
System.Net |
Programmable access to network protocol |
My.Computer |
Gives access to local computer |
My.User |
Gives access to the local user logged in |