Link to home
Start Free TrialLog in
Avatar of Craig Beamson
Craig BeamsonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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")
User generated imageIntellisense 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:
User generated image
The sample C# code that I am trying to emulate (note the difference in how Visual Studio displays the class)
User generated image
Avatar of Craig Beamson
Craig Beamson
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!