In Visual Studio 10 – Visual Basic, why can’t I import “System.Drawing” when my only reference is “System”? I can import “System.Runtime.InteropServices”.
To reproduce my problem:
1. Create a New Project in Visual Studio 10 with Visual Basic Class Library template.
2. Add “Imports System.Drawing” and “Imports System.Runtime.InteropServices” at the beginning.
3. Remove all references except “System” in the References pane of the project properties.
Result:
Visual Studio cannot find “System.Drawing” but it can find “System.Runtime.InteropServices”. “System.Drawing” is fully qualified so the system should be able to find it inside the referenced “System”.
Thoughts:
It appears “System” and “System.Drawing” are distinct namespaces (or containers?) so why doesn’t qualification “.” work? Does “.” represent something else?
“System” is also in “mscorlib” but is that the namespace which is used or is it another?
“Microsoft.VisualBasic” is also listed in the imported namespaces but there is not a reference to it. How was it found? Where is the “Imported namespaces” list populated from?
A link to any related info from the MSDN library would definitely be helpful. I have been looking through it for a while but cannot understand why “System.Drawing” is not imported.
The .NET Common Language Infrastructure has two distinct concepts:
System.Drawing, which are used to distinguish multiple types which would otherwise have the same name.Namespaces form a hierarchy based on full-stop (dot) separators – so you are meant to think that the types in the
System.Runtime.InteropServicesnamespace are subordinate to the ones in theSystem.Runtimenamespace. However, as far as I know, the CLI does not care about the name or hierarchy of your namespaces, except insofar as they make your type names unique.Furthermore, an assembly can contain types from multiple namespaces, even ones in different hierarchies, and a single namespace can contain types defined in multiple assemblies. If you look at the MSDN documentation for a type in the .NET libraries, it will tell you what assembly that type is in. However, as Paolo Falabella has pointed out, MSDN will not tell you what assembly a namespace is in, because a single namespace can contain types from multiple assemblies.
In your scenario:
mscorlibis an assembly that defines some types in theSystemnamespace and many others, such asSystem.Runtime.InteropServices, as you noted. However, the types you are using in theSystem.Drawingnamespace are located in theSystem.Drawingassembly.Because assemblies are the unit of code deployment and reuse, Visual Studio projects reference assemblies, not namespaces, and so you must add a reference to the System.Drawing assembly in the Visual Studio project for your program.
The VB.NET
Importsstatement (and its C# equivalent, theusingdirective) let you refer to types in a namespace without having to type out the namespace name every time. That is, withImports System.Drawing, you can writeGraphicsin your code instead ofSystem.Drawing.Graphics. But that is all the Imports statement does. In particular:Imports Systemdoes not automatically create project references to every assembly in the world that happens to define types in the System namespace.Imports mscorlibdoes not mean you reference every type in the “mscorlib” assembly by its short name. It means you can reference types in the “mscorlib” namespace by their short names, which is not only entirely different but very unlikely to be what you want.Bottom line: if you want access to GDI+, then you use the types in the System.Drawing namespace, but that name has nothing to do with the GDI+ assembly’s name. Microsoft chose the name “System.Drawing” for the assembly that contains GDI+ types, but it could have chosen “gdiplus-cli”, “gdi-for-dotnet”, or even “Frobinator”. Whatever name that assembly has, you have to add a reference to that assembly. And you don’t do that in your source code – you add assembly references in your Visual Studio project configuration.
MSDN has an outdated but still good description of assemblies, namespaces, and the differences between them, which you may find helpful.