views:

289

answers:

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

A: 

Setting a namespace in VB.NET is pretty much the same as declaring a namespace in C#, just with VB.NET syntax! Unfourtunatly 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!

Stevo3000
Try out what you wrote then go in Object browser and look it up.
Shimmy
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
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
That's right my friend.
Shimmy
+2  A: 

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
+1  A: 

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.

Rob Sobers
A: 

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