2.2. Namespaces

All classes are members of some namespace. You can think of a namespace as a user-defined scope—an organizational construct that allows you to group your classes in a meaningful way and uniquely identify your classes and their members in case of naming conflicts. The Hello class from Example 2-1 is a member of a namespace called Greeting denoted by the Namespace block surrounding its definition. Even if the namespace block were removed, the Hello class would still be considered a member of the root namespace, which is scoped to the executable.

2.2.1. Imports

Every time anything is compiled with VB, two class libraries—mscorlib.dll and Microsoft.VisualBasic.dll—are referenced implicitly. The latter contains classes that provide backward compatibility to earlier versions of Visual Basic, while the former contains portions of the System and several other namespaces. Notice the second line of code in Example 2-1:

Imports System

This line brings the System namespace into the scope of the current file, hello.vb. This is done for the benefit of the call to Console.WriteLine, which writes a message to the console window. Without the Imports directive, the Console class could be referred to only through its namespace, which means the call would look like this:

Public Class Hello
    Public Sub Write(ByVal value As String)
        System.Console.WriteLine("Hello, {0}!", value)
    End Sub
End Class

This particular situation is not too bad, but if the file contained several calls ...

Get Object-Oriented Programming with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.