Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to escape root namespace in VB?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Text.MyCustom mc = new System.Text.MyCustom();  
        }
    }
}

namespace System.Text
{
    public class MyCustom { }
}

How do I do this in VB, while having a root namespace in the application, is this possible?

Update: According to the answers I assume there ain't no way to do this. I posted a feature suggestion to Microsoft Connect: Please vote.

like image 976
Shimmy Weitzhandler Avatar asked Aug 21 '09 10:08

Shimmy Weitzhandler


People also ask

What is the root namespace in VB NET?

VB.NET automatically prefixes the root namespace set in the project properties to the namespace of each class. This is different from C#, where the full namespace must be declared each time.

How do I change the root namespace of a project?

In a namespace declaration, Global cannot be nested in another namespace. You can use the Application Page, Project Designer (Visual Basic) to view and modify the Root Namespace of the project. For new projects, the Root Namespace defaults to the project name.

How do I define a namespace in Visual Studio?

You can also use the Global keyword in a Namespace Statement. This lets you define a namespace out of the root namespace of your project. All namespaces in your project are based on the root namespace for the project. Visual Studio assigns your project name as the default root namespace for all code in your project.

What is the default namespace of a VB project?

Before getting into some of the other features, be aware that every project is contained in a namespace. VB.NET uses the name of your project (WindowsApplication1 for a standard forms application if you don't change it) as the default namespace.


1 Answers

In VB.NET of VS 2012 this old problem is fixed. Beginning with this version you can escape the root namespace with a leading global. The following code is buggy in VS 2010 and correct in VS 2012:

Imports Tools

Module Module1
    Sub Main()
        SayHello()
    End Sub
End Module

Namespace Global.Tools
    Module TestModule
        Sub SayHello()
            Console.Out.WriteLine("Hello")
        End Sub
    End Module
End Namespace
like image 135
Dirk Brockhaus Avatar answered Oct 13 '22 00:10

Dirk Brockhaus