ruedi
ruedi

Reputation: 5555

How to use namespaces properly in vb.net

I have a WebApplication project called WebApp and I have a Library Project called LibProj where I define a namespace like

Namespace LibProj
    Public Class Employee
        Public Property Name As String
    End Class
End Namespace

Now I add the reference to WebApp and try to use the namespace with

Imports LibProj

but this does not work. I have to add

Imports LibProj.LibProj

In all the examples I studied it always works with Imports LibProj instead but I just cannot see what is wrong with my procedure. Could anyone help me here?

Upvotes: 3

Views: 3370

Answers (2)

James Thorpe
James Thorpe

Reputation: 32222

In VB, you don't need to specify a namespace in each file - it will take on the root namespace of the project.

These root namespaces have the same name (by default) as the project they're in, so your root namespace will be LibProj already. By adding the Namespace statement in your code, you're adding a sub-namespace to the root namespace.

If you want to create new namespaces outside of your root namespace, prepend it with Global, eg:

Namespace Global.SomeOtherLib
  '...
End Namespace

This is then no longer part of the root namespace of your project.

You can find and edit the root namespace of your project by going to the project properties, within the Application section.

Upvotes: 7

Blackwood
Blackwood

Reputation: 4534

Each project already has a root Namespace which by default is the same as the name of the project (LibProj in this case). When you declare a Namespace for your class (LibProj in this case) that is assumed to be a sub-Namespace within the project's root Namespace. So you have declared the Namespace to be LibProj.LibProj.

If you are happy with all the classes in your project being in the LibProj Namespace, then you don't need to specify the Namespace in each class definition.

Upvotes: 0

Related Questions