views:

1859

answers:

5

Short of cutting and pasting, is there a way to sort the methods in my classes in Visual Studio 2008? I like orderly code.

+1  A: 

You may find or be able to make a macro to do this, but there is no built in functionality of VS to sort your methods. Some third party productivity tools like Resharper and CodeRush provide some functionality to reorder your code.

Kilhoffer
+1  A: 

ReSharper has Code Reordering functionality and a File Structure view that lets you do drag and drop reordering.

bdukes
A: 

Resharper will do a good job in a limited way. It depends on how much you want. For example, it wont go and reorder your overrides in an asp.net page based on lifecycle, or anything like that, but it will keep properties, fields, methods and what not clearly grouped

EDIT: By the eway i was refering to auto reordering aka reformatting.

mattlant
+7  A: 

This is a free plug-in that does what you are asking: http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=800978aa-2aac-4440-8bdf-6d1a76a5c23c

torial
There is a screencast that demonstrates the product here: http://www.rauchy.net/regionerate/2007/06/regionerate-101.html
Shane
+1  A: 

If you are using Resharper, you can change the Type Members Layout template so that it orders your code however you like. See under Resharper>Options>Languages>C#>Type Members Layout. alt text

You can, for example, put methods with particular attributes first in your file... e.g. methods marked with NUnit's [Setup] and [TearDown] could come before methods marked with [Test] by placing a block like:

<!--Fixture Setup/Teardown-->
<Entry>
  <Match>
    <And>
      <Kind Is="method"/>
      <Or>
        <HasAttribute CLRName="NUnit.Framework.TestFixtureSetUpAttribute" Inherit="true"/>
        <HasAttribute CLRName="NUnit.Framework.TestFixtureTearDownAttribute" Inherit="true"/>
      </Or>
    </And>
  </Match>
 </Entry>

before:

<!--Test methods-->
<Entry>
  <Match>
    <And Weight="100">
      <Kind Is="method"/>
      <HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false"/>
    </And>
  </Match>
  <Sort>
    <Name/>
  </Sort>
</Entry>

and then have a catch-all for everything else:

<!--All other members-->
<Entry>
  <Sort>
    <Name/>
  </Sort>
</Entry>

The template system is very powerful and should meet your needs.

Handcraftsman