Classes, structures, modules, enumerations, interfaces, and delegatesthe major .NET typesdon't just float around in the code of your application. They must all be grouped and managed into
. As described in Chapter 1, namespaces provide a hierarchy for your types,
At the very root of the hierarchy is Global , not a node itself, but a Visual Basic keyword that indicates the root of all roots. You can include Global when referencing your namespaces, but its use is only required when leaving it out would cause confusion between two namespace branches.
Directly under Global are the few top-level namespaces, including System and Microsoft . Each top-level namespace contains subordinate namespaces, and each of those can contain additional third-level namespaces, and so on. Namespaces nodes are referenced relative to each other using a "dot" notation.
This specifies the third-level Forms namespace. You could also have typed:
which means the same thing. Relative namespaces are also supported.
However, to use relative namespaces, you must tell your Visual Basic code to expect them. There are so many namespaces out there, and there may be several Forms namespaces somewhere in the hierarchy.
Before namespaces can be used in your code, they must be
Figure 2-3. References and imports for a project
Actually, you are not referencing the namespaces in the DLL, but the types, all of which happen to live in specific namespaces. However, for the
If you don't reference a DLL in your project, none of its types will be available to you in your code. Visual Studio loads several references into your project automatically based on the type of project you create. Figure 2-3 shows the four default references included within a Windows Forms application: System , System.Deployment , System.Drawing , and System.Windows.Forms .
Once you have referenced a library of classes in your code, access any of its classes by specifying the full namespace to that class. For instance, the class for an on-screen form is referenced by System.Windows.Forms. Form . That's three levels down into the hierarchy, and some classes are even deeper. I hope that your health insurance plan covers carpel tunnel syndrome.
To avoiding typing all of those long namespaces over and over again, Visual Basic includes an
feature. Imports are namespace-specific; once a namespace has been imported, you can access any of the types in that namespace without specifying the namespace
You can also import a namespace directly in your source code. Use the Imports statement at the very start of a source code file.
statement supports namespace abbreviations, short
Imports Fred = System.Windows.Forms
lets you reference the Form class as "Fred.Form." Unlike the imports list in the project's properties, which impacts the entire project, the Imports statement affects only a single source code file.
Namespaces in Your Project
By default, all of the classes and types in your project appear in a top-level namespace that takes on the name of your project. For a Windows Forms application, this default namespace is called
. To specify a different top-level namespace, modify it through the
tab of the project's properties, in the
field. All of the types in your project appear in this namespace; if you specify an existing Microsoft-supplied namespace as your project's Root namespace, all of your types will appear in that specified namespace, mixed in with the
From the root namespace, you can place types within subordinate namespaces by using the Namespace statement. Namespace is a block statement that ends with the End Namespace clause. Any types you create between the Namespace and End Namespace clauses will be contained in that subordinate namespace. For example, if your root namespace is WindowsApplication1 , the following statements create a class whose full name is WindowsApplication1.WorkArea.BasicStuff.BusyData :
Namespace WorkArea.BasicStuff Class BusyData ... End Class End Namespace
You can include as many Namespace statements in your code as needed. Nesting of namespaces is also supported.
Namespace WorkArea.BasicStuff Namespace BasicStuff Class BusyData ... End Class End Namespace End Namespace
Expert One-on-One Visual Basic 2005 Design and Development
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (O'Reilly))
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
Beginning VB 2005 Databases: From Novice to Professional (Beginning: From Novice to Professional)