0

I'm a complete newb. I see that these using statements or references at the top of the page are a pain to manage. Is there a way to reference these in a class or some other file and have a single statement at the top of the form code or in a logic file?

Ex: Make this

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Windows.Forms;

into this

using Class : FileName

Or some other form of reference.

Not having fully mapped the program I frequently move things to the logic files. Occasionally I get an error such as Are you missing a using directive or namespace. Usually I see I'm missing something that was where I created the function but is missing where the function now resides. I keep a master and just copy it around to all the files when this occurs. Usually clears the error.

Seems like there must be a better way.

Thank you in advance. I appreciate any input and help I can get. Have a Great Day!!

Frank Pytel
  • 127
  • 16
  • 1
    The most pragmatic solution is to just let visual studio take care of using directives. Use the remove unsed usings and the sort usings features to organize your usings. – SBI Nov 10 '14 at 13:38
  • 1
    You can tidy up your usings in visual studio so that they only contain ones that are actually needed Edit => IntelliSense => Organise Usings => remove unused usings. – Ben Robinson Nov 10 '14 at 13:40
  • 2
    Resharper makes moving stuff easier as it will move using statements as well or suggest adding them. – juharr Nov 10 '14 at 13:41
  • @BenRobinson and the other person it will not let me include. Thank You. :-D Brilliant. Ben that was very close to what I was looking for. At least that gets rid of the junk. But how do I know exactly what statement might be missing? My guess is a 100% complete list pasted in and then use your trick Ben? – Frank Pytel Nov 10 '14 at 13:42
  • 1
    You normally get an error if you try to use something that is missing, and you'll get an IIntellisense marker on it that you can drop down to either fully qualify the type, or import the relevant namespace. – Rowland Shaw Nov 10 '14 at 13:43
  • 1
    If you put your cursor at the end of the function or declaration that's giving you the problem and hit Ctrl + . [period] VS finds out what you're missing and gives you the option to add the missing using statement. – Chris Searles Nov 10 '14 at 13:43
  • @ChrisSearles Very Very Good!! Thank You :-D – Frank Pytel Nov 10 '14 at 13:45
  • Another option is discussed on the following thread: http://stackoverflow.com/questions/125319/should-using-statements-be-inside-or-outside-the-namespace – James Shaw Nov 10 '14 at 13:52

1 Answers1

4

ReSharper makes this trivial (almost to the point of being dangerous), it allows you to insert them with Alt+Enter and also highlights unused ones. VS also comes with basic support via right-click. Basically, I've never felt the need for the feature you describe - which doesn't exist so far as I'm aware.

I tend to sort mine alphabetically in two categories: .NET's System namespaces first, everything else second. A third category could also separate internal from external namespaces, but this tends to be a little overkill for most situations.

However, take the following approach: if you really have that many using statements, the contents of that code file (assuming you do using declarations outside of namespace declarations) might be Doing Too MuchTM. If your types are well targeted with as few responsibilities as you can get away with (read: aim for one), then you won't have many using statements anyway.

This naive code-smell alert mostly works, but you will have the odd type that has a proliferation of using declarations, so don't treat it as some sort of catch-all.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187