开发者

Visual Basic and a Namespace Issue

I was cleaning up a Visual Basic (.NET 2.0) solution. Splitting it into two projects. 150 classes have the Namespace RebateCalculator at the top of the file. These files are now sitting in Project with the default namespace RebateCalculator. If I were to insert a Class1.cs file and then namespace declaration that all my other files have - then the fully-qualified class name would be RebateCalculator.RebateCalculator.Class1

Is there something I can stick on the front of the Namespace declaration to make it absolute? instead of repeating itself? I'd rather find this kind of solution开发者_JAVA百科 versus removing the namespace declaration (in case the files get moved again)

End Goal: to be able to do a search/replace on 'Namespace RebateCalculator' to fix the issue in 150 files.


I don't know if this answers your question but just in case...

Open the Project Properties and click on the Application properties. CLEAR the Root Namespace there. Then your Namespace declaration in each file will be "absolute".

By the way...this is very likely to break your application and could take a while to get everything fixed up. Be sure to backup (or at least be committed to your repo) before starting.

As an aside. I think that combining a Project Root Namespace with a declared namespace is confusing. I usually do one or the other.

Seth


The duplicate answer you posted alluded to the problem.

VB.Net has the concept of a "Root Namespace". When you add a Namespace statement to the code file, the name given is combined with the root namespace to form the full namespace.

So, in VB.Net, if your root namespace is MyRoot and then you create a class and surround with a namespace of MyNamespace, then the full class name is MyRoot.MyNamespace.ClassName. If you don't use a Namespace statement, then the full class name is MyRoot.ClassName. In other words, the root namespace setting contributes to the name of the namespace.

In C#, this is not the case. C# has a "default namespace". This is simply a namespace definition that is automatically added to new classes when you create them. If you remove the namespace declaration, then the class has no namespace. In other words, the default namespace setting does not actually contribute to the name of the namespace.

In C#, whatever is on the namespace declaration is the namespace used. The default namespace setting only determines what is inserted by default in the code file.

Take this VB program that has a Root Namespace setting of "TestVBConsole" and no namespace statement in the class:

Module Module1
    Sub Main()
        Dim tc As New TestClass

        Console.WriteLine(tc.GetType().FullName)

    End Sub
End Module

and this class

Public Class TestClass
    Public aField As String = "Default"
End Class

The output is

TestVBConsole.TestClass

The same program in C# outputs

TestClass

As to your question, I don't think there is any way to override that behavior. You just have to remember in C#, to make sure your namespace declarations are correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜