13
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.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • 2
    You cannot do this without removing the root namespace for the application. The only way to achieve what you want is to remove the root namespace, and explicitly set namespace everywhere in the code. In C# project you don't have root namespace, only default namespace, which is really what you're asking for here. Sorry, this is one of the differences between VB.NET and C#. – awe Aug 21 '09 at 11:25
  • 2
    to awe: this should be an answer below.. – Colin Aug 21 '09 at 11:28

5 Answers5

14

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
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Dirk Brockhaus
  • 4,922
  • 3
  • 38
  • 47
  • 1
    It was [my suggestion on connect](https://connect.microsoft.com/VisualStudio/feedback/details/484455/allow-escaping-root-namespaces-in-vb), I'm so proud it graduated!! – Shimmy Weitzhandler Jun 28 '13 at 08:30
5

I think that the sad truth is that you can't. The namespaces are appended to the root namespace. The documentation gives no hint of any escaping mechanisms. There is a note about using the Global keyword in relation to namespaces, but I interpret that part of the text as dicussing how to refer to namespaces rather than how to declare them.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
1

Take a look this question: Possible to override VB.NET root namespace?.

The bottom line is that your only option is to leave the default namespace empty in the project properties and then wrap all of your class/module definitions in Namespace statements.

Community
  • 1
  • 1
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
0

Setting a namespace in VB.NET is pretty much the same as declaring a namespace in C#, just with VB.NET syntax! Unfortunately the root namespace is always present so any new namespaces declared will be inside the root namespace.

Namespace ConsoleApplication1
  Class Program
    Private Shared Sub Main(ByVal args As String())
        Dim mc As New System.Text.MyCustom()
    End Sub
  End Class
End Namespace

Namespace System.Text
  Public Class MyCustom
  End Class
End Namespace

The above code will give you the following if the root namespace is Test.

Test.ConsoleApplication1
Test.System.Text


Cheers for the comments guys, was posting on memory!

KyleMit
  • 30,350
  • 66
  • 462
  • 664
stevehipwell
  • 56,138
  • 6
  • 44
  • 61
  • 1
    Try out what you wrote then go in Object browser and look it up. – Shimmy Weitzhandler Aug 21 '09 at 10:24
  • 1
    It's being added to ConsoleApllication1.System.Text, unlike in C#. Of course, I can set the root-namespace to null, but not this is what I want. – Shimmy Weitzhandler Aug 21 '09 at 10:27
  • 1
    Furthermore, I would guess that the `Program` class lives in the namespace `ConsoleApplication1.ConsoleApplication1` in the above example (I don't have a vb.net environment around to verify it though). – Fredrik Mörk Aug 21 '09 at 10:32
0

I the Project properties of a VB project you can change the root namespace. This is per default same as the project name, but you can remove it, and then you have full power of the namespace structure in code. The drawback is that you have to specify the project name as namespace everywhere in the code where you need it...

For C# projects, the similar setting in th Project properties, is only the Default namespace, which is overridden if you specify namespace in the code. For VB projects it specifies the Top level namespace, not the default....

awe
  • 21,938
  • 6
  • 78
  • 91