Managing Namespaces in VB.Net
A namespace is a way to disambiguate one public interface from another. You can happily go through life not worrying about explicitly declaring namespaces in your VB.Net code because of VB.Net's automatic namespace creation. So you can create a project, give it a name, and start writing code. Beautiful and blissful.
Then one day, you wake up and the solution your team works on won't compile.
This will happen to you as a result of not managing your projects' namespaces properly. Eventually, you will need to understand what VB.Net is doing. And fix it.
A VB.Net project has a root namespace property, set by default to your project's name. For example, in a project named "VBNetLib," you have a class called "MyUsefulClass" that looks like this:
Public Class MyUsefulClass
Public Shared Sub DoesSomethingGood()
End Sub
End Class
You can access the DoesSomethingGood() method from another project like this:
VBNetLib.MyUsefulClass.DoSomethingUseful()
In the VBNetLib project, you'll see the Root Namespace property set to "VBNetLib." That is why you must call DoSomethingUseful as shown above, i.e., in the form namespace.class.method.
One day, let's suppose you read a C# book and notice every single class, without exception, has the namespace explicitly in the source code:
namespace CSNetLib
{
public class MyUsefulClass
{
public MyUsefulClass()
{
}
public static void DoesSomethingUseful()
{
}
}
}
You right-click on the C# project's properties and see a Default Namespace property, "CSNetLib." Since all those smart people are using C# nowadays, you decide to mimic this and modify your VB.Net class as follows:
Namespace VBNetLib
Public Class MyUsefulClass
Public Shared Sub DoesSomethingGood()
End Sub
End Class
End Namespace
Feeling much better, you compile the solution and see a build error: "'MyUsefulClass' is not a member of 'VBNetLib'. That is because you have just created a nested namespace, VBNetLib.VBNetLib, and the method can only be called this way:
VBNetLib.VBNetLib.MyUsefulClass.DoesSomethingUseful()
C# and VB.Net have similar looking project properties, but they are not equivalent. C# requires an explicit namespace in all source code, and a C# project's default namespace automatically adds this to new classes. VB.NET does not require an explicit namespace in source code, and a VB.Net project's root namespace is not used as a template for new source code but is instead a second way to specify a namespace in VB.Net projects. The other way is like C#: explicitly add a namespace declaration to your source code as shown above.
Many people recommend (and I will too -- note that some do not) that you set a VB.Net project's default namespace to blank (i.e., delete whatever is there) and instead explicitly declare (yes, manually type in) a project's namespace in all source code. This will make it easier to manage the scope of your classes and will make obvious how to access code in referenced assemblies. Spending time chasing down a nested namespace problem is not one of the joy's of VB.Net programming. Try to avoid ever encountering it.
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1034634/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 2.2.1.2 Namespaces in a CDBnamespace
- 8-Overview-NamespacesViewnamespace
- Kubernetes Namespacesnamespace
- Kubernetes – Namespacesnamespace
- iOS:Managing the KeyboardiOS
- 管理Managing SQL ProfilesSQL
- kubernetes traefik multiple namespacesnamespace
- Managing Startups: Best Blog Posts
- Managing Rails Apps at Massive ScaleAIAPP
- android tv-Managing User InteractionAndroid
- Managing Non-Volatile Memory in Database SystemsDatabase
- 實錘,PBlaze5擁有multiple namespaces能力(上)namespace
- 【OH】3 Managing Oracle Cluster Registry and Voting DisksOracle
- 播布客視訊-Managing Indexes筆記Index筆記
- 精通ASP.NET(基於VB.NET)( 二)VB.NET類 (轉)ASP.NET
- 精通ASP.NET(基於VB.NET)(四)VB.NET繼承 (轉)ASP.NET繼承
- vb.net 讀取ExcelExcel
- VB.NET 總結一
- VB.NET聊天程式 (轉)
- 實錘,PBlaze5實力演繹multiple namespaces 功能(下)namespace
- 【翻譯】控制檔案管理(Managing Control Files)
- 精通ASP.NET(基於VB.NET)( 三)VB.NET異常處理 (轉)ASP.NET
- Managing multiple archive log destinations with RMAN (Doc ID 443814.1)Hive
- The Self-Managing Database: Automatic Performance Diagnosis(一)原文翻譯DatabaseORM
- 網路虛擬化基礎一:linux名稱空間NamespacesLinuxnamespace
- Managing Optimizer Statistics(轉自ORACLE 10G TUNING GUIDE)Oracle 10gGUIIDE
- VB.net chart 控制元件使用控制元件
- VB.NET 讀寫ini檔案
- Henry的VB.NET之旅(八)—介面
- 【VB.NET視訊總結(三)】
- 【VB.NET視訊總結(一)】
- 【VB.NET視訊總結(二)】
- VB.NET中物件的克隆 (轉)物件
- Oracle 20c 新特性:自主的 In-Memory 管理 - Self-ManagingOracle
- Managed C++: Another VB, or VB.NET, or WhateverC++
- vb.net 類庫中如何使用webserviceWeb
- VB.NET重新申明陣列簡介陣列
- 簡單描述VB.NET申明陣列陣列