Visual Studio VB - Having to include the project name in "Import myNamespace" declarations

Beamson
Beamson used Ask the Experts™
on
I am using namespaces for the first time in a Visual Studio project, following a sample project tutorial and am struggling to use them.
(I usually don't bother specifying namespaces but am trying to do things "by the book"!)

My basic problem is that Visual Studio's intellisense doesn't like me doing a VB Imports statement with just the namespace I specify - it wants me to add the project name to the namespace to validate my code.  When I do this, my code can build successfully but I then get problems further down the line. Before getting to solving these latter problems, I'd like to understand why this namespace/imports behaviour is different behaviour to that of the sample code I'm trying to emulate.

The sample project that I am working with is:
getting-started-with-aspnet-45-web-forms
The complication is that I'm using Visual Studio 2017 and I'm translating all of the code to VB as I go!

In my code, I am specifying a Namespace in a vb file as:
Namespace WingtipToys.Models

    Public Class Product

    End Class

End Namespace

Open in new window


...but when I want to use it in an Imports statement on codebehind for another page, intellisense is insisting that I include the project name (also "WingtipToys")
Intellisense not happy with my code without adding the extra namespace.Intellisense suggests I change it to
Imports WingtipToys.WingtipToys.Models.Product

Open in new window

In the sample project code I am working to, the shorter imports statement works just fine - intellisense quite happy with it - project builds - no errors while debugging.

In my project though:
If I stick to the shorter Imports statement, my code won't build successfully.
If I use the longer namespace declaration in the Imports statement my code builds but fails elsewhere during debug.
I just can't work out why they are being handled differently.  Do I have to "declare" the namespaces somewhere globally?

Also, the way Visual Studio displays my code and the sample code differs very slightly:
My VB code:
My code
The sample C# code that I am trying to emulate (note the difference in how Visual Studio displays the class)
The sample code I am trying to emulate
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Minor update:
The knock-on problems that I was experiencing can be remedied by changing various references in the code which used the shorter namespace to the longer one.  I'd still just like to know why I need the extra word in the namespace to make my version of the code to work.

Adding the extra "WingtipToys." word to the ItemType below gets around my problem but I still don't understand why I need to do that to make things work.
 <asp:ListView ID="productList" runat="server" 
                DataKeyNames="ProductID" GroupItemCount="4"
                ItemType="WingtipToys.Wingtiptoys.Models.Product" SelectMethod="GetProducts">

Open in new window

Software Engineer
Top Expert 2011
Commented:
In vb.net the default root Namespace is the same as your project file, and can be inspected in the Project properties Application section.
This means you don't have to specify a namespace in each file as they automatically belong to the root namespace.
If you want to add a sub-namespace you can and it will be automatically added as part of the root namespace so specifying in your file:

Namespace Models

will then be referenced by WingtipToys.Models, as WingtipToys is the root namespace

Author

Commented:
Easy when you know how!

So in VB.NET, rather than rewrite all my Imports codes, or class references to add the extra namespace tag, I remove the root namespace from my classes and it all magically works.

Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial