Recipe 3.8. Using NamespacesProblemYou want to place your classes within a specific .NET namespace. SolutionUse the Namespace statement together with the default namespace identified in a project's properties. Discussion
Every Visual Basic application resides within a default namespace, what we'll call the "absolute namespace position" for your application. Visual Studio automatically sets this to a top-level namespace with the same name as your project. For instance, if you just accept the default "WindowsApplication1"
To alter the namespace for your project,
When generating a full .NET application (EXE), your choice of namespace is not too
Beyond the absolute namespace position, you can place your classes and other types in a "relative namespace position" within the larger default absolute namespace. When you add a class (or other type) to your project, it appears in the absolute namespace position: Class Class1 End Class If your project uses WindowsApplication1 as its absolute namespace, this class appears as WindowsApplication1.Class1 . In relative positioning, you can insert a new namespace between the absolute position and the class: Namespace CoolClasses Class Class1 End Class End Namespace Now, Class1 is fully referenced as WindowsApplication1.CoolClasses.Class1 .
The
Namespace
keyword may include multiple namespace
Namespace CoolClasses Namespace SomewhatCool.BarelyCool Class Class1 End Class End Namespace End Namespace This Class1 lives at WindowsApplication1.CoolClasses.SomewhatCool.BarelyCool . |
Recipe 3.9. Splitting a Class Across Multiple FilesProblemYou have a class that is simply too much to manage reasonably in a single sourcecode file, and you would like to split it up. SolutionUse the Partial keyword on a class to enable splitting the implementation of that class across multiple physical source files: Partial Class Class1 End Class Discussion
Visual Basic now includes a
partial class
feature that Visual Studio uses to separate automatically generated code from nongenerated code. This feature is available to use in your own classes. Before Visual Basic 2005, if you tried to split a class by using the
Class
statement multiple times on the same class
Class Class1 ' ----- Some class members are defined here. End Class Partial Class Class1 ' ----- More class members are defined here. End Class
The key is the word
Partial
. Adding the keyword
Partial
to at least one of the class
You do not need to include Partial on every part of the class, just on one of the parts. Also, if your class inherits from another class or implements an interface, you need to include only the Inherits or Implements keyword in one of the class portions. All class parts must exist in the context of the same namespace. If you create different class definitions with the same name but in different namespaces, they will be distinct and unrelated classes. |