views:

197

answers:

1

Using import aliasing in one file/class, we can reference class library namespaces by assigning our own custom alias like this:

' VB
Imports Db = Company.Lib.Data.Objects 

// C#
using Db = Company.Lib.Data.Objects;

And then we are able to reference the classes inside of Company.Lib.Data.Objects by using the Db alias that we assigned.

Is it possible to do this at the global level so that the alias is applied to the entire solution instead of just one file/class?

Currently, we are working with web applications, so I was hoping we could add something to web.config, but I am also interested in whether or not this is possible with windows forms, console apps, and/or class libraries.

+3  A: 

Yes this is a supported scenario in VB.Net projects. The way to do this is the following

  • Right Click on the project in Solution Explorer and select Properties
  • Go to the References tab
  • In the "Imported Namespaces" field type "Db=Company.Lib.Data.Objects"
  • Hit "Add User Import"

This will set up the alias for all files in the project.

This however does not work in C# projects. C# as a language doesn't have the concept of a global using/import. Instead it only supports them at the file level.

JaredPar
+1 - thorough answer.
Jay Riggs
Awesome! We use VB.NET exclusively (dont laugh!), so that is fine. But, does this work in "Web Sites" or only "Projects"?
Josh Stodola
@Josh most of my job involves working on the VB.Net IDE so no laughing here. This will only work though on web applications. I do not believe it will work for web sites as they don't have the notion of a global build.
JaredPar
@Jared We really love using VB.NET here, so keep up the great work! Thanks for the answer, this is exactly what I was looking for.
Josh Stodola