VB.Net projects allow you to specify project-wide namespace imports. These can be seen on the References page of your project's propertes. It's likely that your first project, P1, has a project-wide import of System.Data.SqlClient whereas P2 does not.
importing namespace and using their hierarchy
-
09-07-2023 - |
Question
I always thought including/importing/using a namespace would include all the namespaces beneath it but I'm having a problem importing namespaces in Visual studio 2012.
I'm using VB.net and have two projects inside one solution file. I'm moving a class that used to be inside of P1 into P2. The code inside of P1 had just "Imports System.Data" but in the new project/class I can't say that I have to be specific and say "Imports System.Data.SqlClient" or else visual studio doesn't know what SqlConnection is
Imports System.Data
'Imports System.Data.SqlClient
Public Class Class1
Private con As SqlConnection
Public Function returnInt2() As Integer
Return 2
End Function
End Class
My question is two fold
1.Should "Imports System.Data" also import "System.data.sqlClient"?
2.Does anyone have an idea of why I have to be specific in my imports in P2 but not P1?
Solution
OTHER TIPS
No import a namespace will not import all child namespaces. If you did, there could be hundreds and hundreds of namespaces down the line.
Importing a namespace will give you access to all child members of that specific namespace.
The reasoning here is that you have to package all the libraries that those namespaces encompass. Therefore if you imported every namespace ever, your binary would get pretty big.