KnowDotNet NetRefactor

Using Project Level Imports in VB.NET.

by Les Smith
Print this Article Discuss in Forums

Is there a shortcut way to have all of the Imports that I need in a class, in VB.NET, without having to worry about adding Imports to every class?  For example, when I need to code a DoEvents statement, I find myself having to either place an "Imports System.Windows.Forms.Application" at the top of every class where I want to use DoEvents or else I have to code System.Windows.Forms.Application.DoEvents in-line in my code.  The same thing occurrs many times with the use of StringBuilder.  I have to go back and place Imports System.Text or code Text.StringBuilder (System always seems to be there).  

Yes, there is a shortcut, for the same reason System always seem to be there.  In the Property Pages dialog for a VB.NET project (not available in C#), you can add the Namespaces that you plan to use in your project.  When your project is created by Visual Studio .NET, it has several Imports included for you.  To add others that you need all the time, click on the Project Menu and then click the Properties option.  Alternately, you can right-click on the project in the Solution Explorer and then click on the Properties menu option.  Either method will result in the display of the Project Property Pages dialog, shown in Figure 1.  Click on the Imports property to insert new Imports as shown in Figure 1.  When you start typing a new Namespace in the Namespace text box, the Add Import button will be enabled.  Any Imports that you place here will no longer be required in individual classes.

Figure 1 - Project Property Page Dialog.

Project Property Pages Dialog


Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing