Moby Disk Consulting
Software Development, Training & Consulting
William Garrison - mobydisk at mobydisk daht com

Macro to Tidy C# variable declarations

I created a macro that automatically lines-up variable declarations to look pretty. I find myself doing this often before code reviews, or when going through someone else's code so having a macro to do it for me is very nice. Take a look:

Before

      private int _numberOfSharks;         // Does not include dolphins
      private double _watts;    // Power of each laser
      private List<string> _sharkNames;

After

      private int          _numberOfSharks; // Does not include dolphins
      private double       _watts;          // Power of each laser    
      private List<string> _sharkNames;

Click here to download the Tidying.vb macro.

The macro isn't very smart. If the declarations are different then it won't line them up propertly. For example, if some lines don't have the access modifier, or some have attributes. It only recognizes // style comments. If enough people use this then I will enhance it.

To use this macro, go to Tools - Macros - Macros IDE. Then paste the contents of the file above into a new Module named Tidying. Save it, then go into Tools - Options - Environment - Keyboard. Assign some unused key like ctrl-alt-shift-T to Tidying.TidyVariableDeclarations. Then, go highlight a block of code that declares variables and press your hot key.

Visual Studio macro setup screen shot