16

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.

Is it possible to override this behaviour, creating a namespace outside of the root namespace?

  • 1
    This problem is solved in VS 2012. See my answer to this question http://stackoverflow.com/a/17360357/233095 – Dirk Brockhaus Jun 28 '13 at 08:27
  • possible duplicate of [Is there a way to escape root namespace in VB?](http://stackoverflow.com/questions/1311099/is-there-a-way-to-escape-root-namespace-in-vb) – MaxiWheat Feb 06 '14 at 18:28

4 Answers4

12

If I understand you correctly you just need to set a blank namespace in the project properties dialog and then set namespaces within each source file using Begin/End Namespace commands.

From VS2012 onwards it's possible to get around this, see stackoverflow.com/a/17360357/233095

Community
  • 1
  • 1
RobS
  • 3,807
  • 27
  • 34
5

They now implemented it:

Namespace Global.MyNamespace

End Namespace
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
5

Defining the default for the project as blank and then taking total control in each class allows you to do what c# does. However certain project types (Library I believe) do not allow you to change the default namespace to blank.

Use of the Global keyword does not allow you to jump out of the root namespace either: http://msdn.microsoft.com/en-us/library/16czfx55.aspx

ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
  • According to the [latest MSDN article](http://msdn.microsoft.com/en-us/library/0dx91cw5(v=vs.120).aspx): *The Global keyword can also be used in a Namespace statement. This lets you define a namespace out of the root namespace of your project. For more information, see the "Global Keyword in Namespace Statements" section in Namespaces in Visual Basic.* See Shimmy's answer... – Steven Doggart Mar 06 '14 at 16:20
4

You can change the namespace of the entire project by going to properties on the project. else you will have to have a empty root namespace and set the name space in each file with the

Namespace test
    class.....
End Namespace
Peter
  • 37,042
  • 39
  • 142
  • 198