Home
Map
Namespace ExampleUse Namespaces to logically organize code. Specify the Imports and Namespace keywords.
VB.NET
This page was last reviewed on Jan 14, 2024.
Namespace. This is an organizational construct. It cannot be instantiated (as with New) like a Class. Instead, we use a namespace to separate code into logical parts.
Namespace benefits. Namespaces help developers understand code but do not impact execution. They can make a program easier to develop and learn.
Imports
This example program is divided into 3 classes. The first 2 classes use custom namespaces—Perls and Ruby. They both contain classes—two independent classes both named Website.
Info In the Properties window, we can set a "Root namespace." I set it to ProgramExample. This contains the other namespaces.
Next In the third part, we see the Imports directive. We include the Perls namespace, which is nested under the Root namespace.
Finally We access the Ruby namespace directly with a composite name. No Imports statement is needed.
Namespace Perls Class Website Public Shared Sub Execute() Console.WriteLine("Perls Website") End Sub End Class End Namespace
Namespace Ruby Class Website Public Shared Sub Open() Console.WriteLine("Ruby Website") End Sub End Class End Namespace
Imports ProgramExample.Perls Module Module1 Sub Main() ' This requires the Imports ProgramExample.Perls directive. Website.Execute() ' Access namespace directly in a statement. Ruby.Website.Open() End Sub End Module
Perls Website Ruby Website
Discussion. There are many ways to use namespaces in programs. Every program by default uses a Root namespace. This is automatically generated when we create a new project.
Tip It can be changed by going to the Project menu and selecting Properties. And we can specify new namespaces with the Namespace keyword.
Tip 2 To access types within a Namespace, we use Imports or a direct access with a composite name (like Ruby.Website.Open).
Summary. Projects often have special rules for Namespaces. Many projects use company-based identifiers in Namespaces. Naming rules are specified by the project, not the VB.NET language itself.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 14, 2024 (edit link).
Home
Changes
© 2007-2024 Sam Allen.