navigation
 Wednesday, December 17, 2008

Have you ever been in the middle of large source file and you need to add a "DataTable" or something else, but you don't have the appropriate "using" directive at the top of your file? In VS.NET it is very simple to add the "using" directive with a few clicks saving us developers some valuable keystrokes.

In the shot below you see my simple class where I have added a new DataTable variable and the editor is warning me that it does not recognize the "DataTable" type.




If you position your cursor anywhere on the text "DataTable" and press the Ctrl + . keys (that's a period after the Ctrl), the SmartTag will pop up and show that "using System.Data;" will solve this compile error. Selecting it from the popup menu will automatically add the "using System.Data;" code at the top of your file.




Another way to accomplish the same thing is to right-click on the "DataTable" text to show the popup menu, then choose "Resolve" and the "using System.Data;" code shows up again, selecting it at this point will insert the reference as well.




The final shot below shows the same code with the newly added directive.




The catch to using these techniques, is you need to have the proper casing of the type you are creating so the compiler can find it and your type will also need to be in the scope of your project (i.e. a reference to the library that contains it.)

Hopefully, this will save you some keystrokes.