Today, I wanted to add some VB.NET code to an existing solution where all of the other projects were written using C#. I had a working NAnt build script for this solution, so I thought it would be a piece of cake to add a target to my build script to build my VB.NET assembly. Boy was I wrong. An hour later, I figured out the answer.
VB.NET does a couple of things to make your life easier. First, it imports a number of commonly used namespaces on a project-wide basis. On my computer, VB.NET automatically imports the following namespaces: Microsoft.VisualBasic, System, System.Collections, System.Data, System.Diagnostics. Second, it assigns a “root namespace” to all types defined in the assembly. The “root namespace” defaults to the name of the project. You can see these settings under Project|Properties in Visual Studio .Net. The interesting tabs are Common Properties|General and Common Properties|Improts.
While these are nice features which save you time, it can really make a mess of your NAnt build scripts which don’t have the same set of default imports. Fortunately, the vbc task in NAnt defines attributes that you can use to define these settings. The first is the imports attribute where you can add the appropriate default namespace imports, as shown below:
The second is the rootnamespace attribute where you can define the default namespace for the project, as shown below:
I hope this saves someone else out there some time.
2 archived reader comments
Ian MacLean (2003-10-10)
Slightly releated is another vb.net issue with nant. In previous versions you had to specify the full path to framework asemblies ( eg. system.xml.dll ). Otherwise they wouldn’t be found. Now if you specify a bare file name nant will look for it in the framework assemblies directory. This change is post 0.8.3 I think. This was never really a problem with csc which uses a response file listing the core system assemblies.
Lance Fisher (2004-05-13)
Hi, this saved me time thanks! One thing - it wouldn’t build if there was a space between assemblies in the imports: i.e. needs to be “VisualBasic,System” not “VisualBasic, System”