This will happen anytime that you are attempting to use a namespace that also exists anywhere within the hierarchy of the currently scoped namespace. Let me explain me with the following example:
Say you have the following code:
<!-- language: lang-vb -->
Namespace Company.Application
Module Module1
Sub Main()
'Code Goes Here
End Sub
End Module
End Namespace
Namespace Company.Application
Public Class ApplicationClass
End Class
End Namespace
Namespace Company.Business
Public Class BusinessClass
End Class
End Namespace
Namespace Business
Public Class RootLevelBusiness
End Class
End Namespace
Now let's see what's exposed when we go to use the Business
Namespace in our Module (we should expect to see the RootLevelBusiness
class
But We Don't!!!
That's because the code has worked it's way up the hierarchy of the current namespace and found a business class before getting to the root. To help prove that, look what happens when we include Company
:
You'll see that Company
is greyed out because it doesn't need it. It would be running the same code with or without qualifying that it comes from Company, because we're already in the Company
.Application
namespace.
Solution - Use Global
If you'd like to qualify classes inline, you need a way of telling the compiler to not look inside of your current namespace when resolving classes. For this, use Namespace Global
which provides:
a new way to “escape” your classes out of the project’s Root Namespace
Global tells the compiler to start from scratch, in which case we immediately find the Business namespace.
The reason you can add the class through the Imports
statement without using Global is because Imports
is global by default. Because you have to declare Imports
before defining any Namespaces
, the imports statement has no way to presume what the namespace will be in any subsequent block of code in the rest of the file because you can (although probably shouldn't) declare as many namespaces as you want within a single file. For this reason, Imports will always start from the root of any namespace and work it's way down.