Craig Beamson
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-aspne t-45-web-f orms
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:
...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 suggests I change it to
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:
The sample C# code that I am trying to emulate (note the difference in how Visual Studio displays the class)
(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-aspne
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
...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 suggests I change it to
Imports WingtipToys.WingtipToys.Models.Product
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:
The sample C# code that I am trying to emulate (note the difference in how Visual Studio displays the class)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
ASKER
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.
Open in new window