TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Namespace Example

Use Namespaces to logically organize code. Specify the Imports and Namespace keywords.
Namespaces are an organizational construct. They cannot be instantiated (as with New) like a Class. Instead, we use them to separate code into logical parts. They help developers understand code but do not impact execution.Class
This example program is divided into three classes. The first two classes use custom namespaces—Perls and Ruby. They both contain classes—two independent classes both named Website. Those two classes are not related or linked.

Root namespace: In the Properties window, we can set a "Root namespace." I set it to ProgramExample. This contains the other namespaces.

Imports: In the third part, we see the Imports directive. We include the Perls namespace, which is nested under the Root namespace.

Main: We access the Ruby namespace directly with a composite name. No Imports statement is needed.

Class 1 that uses Namespace: VB.NET Namespace Perls Class Website Public Shared Sub Execute() Console.WriteLine("Perls Website") End Sub End Class End Namespace Class 2 that uses Namespace: VB.NET Namespace Ruby Class Website Public Shared Sub Open() Console.WriteLine("Ruby Website") End Sub End Class End Namespace VB.NET program that uses Imports, Namespaces 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 Output 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. It can be changed by going to the Project menu and selecting Properties.

Tip: We can specify new namespaces with the Namespace keyword. We can place types like Classes inside a Namespace.

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. For example, I have seen many projects use company-based identifiers in Namespaces. Naming rules are specified by the project, not the VB.NET language itself.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf