HTML clipboardAssemblies are for physical scope and namespaces are for logical scope, so
namespaces can be expanded over assemblies but the converse is not possible.
The namespace keyword is used to declare a scope. This namespace scope lets you
organize code and gives you a way to create globally unique types.
Even if you do not explicitly declare one, a default namespace is created. This
unnamed namespace, sometimes called the global namespace, is present in every
file. Any identifier in the global namespace is available for use in a named
namespace.
Namespaces are hierarchical. They implicitly have public access, which you
cannot modify.
It is possible to define a namespace in two or more declarations. For example,
the code in Listing 5.37 defines both classes as part of namespace MyCompany.
Listing 5.37: NameSpace.VB, Namespaces Example
'
recurring namespaces
Namespace
MyCompany.Proj1
Class [MyClass]
End Class
End Namespace
Namespace
MyCompany.Proj1
Class MyClass1
End Class
End Namespace
'nested namespaces
Namespace
SomeNameSpace
Public Class [MyClass]
Public
Shared Sub Main()
Nested.NestedNameSpaceClass.SayHello()
End Sub
End Class
Namespace Nested
' a nested namespace
Public
Class NestedNameSpaceClass
Public Shared Sub
SayHello()
Console.WriteLine("Hello")
Console.ReadLine()
End Sub
End
Class
End Namespace
End Namespace
The code listing generates the screen output in Figure 5.8.
Figure 5.8: Screen Output from Listing 5.37
VB programs are organized using namespaces, which serve as both an internal
organization system for a program and an external organization system that is, a
way of presenting program elements that are exposed to other programs.
VB also enables the definition and use of aliases. Such aliases can be useful in
situations in which name collisions occur between two libraries, or when a few
types from a much larger namespace are being used. Listing 5.38 presents code
from the preceding example, rewritten to use aliases.
Listing 5.38: NameSpace Assignment Example
Imports
MessageSource = Microsoft.CSharp.Introduction.HelloMessage
Class Hello
Private
Shared Sub Main()
Dim m As New MessageSource()
System.Console.WriteLine(m.GetMessage())
End Sub
End Class
An assembly is everything that comprises a .NET application. It is the unit of
deployment for the Microsoft .NET framework and takes the form of an executable
(.exe) file or dynamic-link library (DLL). What makes assemblies different from
.exe files or DLLs in earlier versions of Windows is that assemblies contain
within them all the information that you would find in a type library along with
information about everything else that is necessary to use the application or
component.
One of the primary goals of assemblies is versioning. Specifically, assemblies
provide a means for developers to specify version rules between different
software components and to have those rules enforced at runtime.
Because assemblies are the building blocks of applications, they are the logical
point for specifying and enforcing version information. Each assembly has a
specific version number as part of its identity.
Conclusion
Hope this article would have helped you in understanding Namespaces and
Assemblies in VB.NET.