Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Method Sorting of VB Source Code

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
16 Jul 2015CPOL2 min read 19.4K   292   1   16
Method sorting utility for VB.NET source code

Introduction

This is a simple command line utility to sort methods in VB source code, it can be used as an external tool from Visual Studio.

Background

I just needed something to sort my methods but not anything else, so I created this simple utility to do the work for me. It could be extended into a fully integrated Visual Studio Extension if someone wanted to do that.

Using the Code

The program is a simple EXE that runs from a command line. The options are:

-f"fullpath to your vb code"

Full path to the source code file e.g. -f"c:\my files\my code.vb"
This can be provided as a command line parameter from Visual Studio as described below:

-sAAB

Sort order for Subs, Functions, and Properties.
When sorting, each character is used to prefix the method name so that Subs, Functions and Properties can be grouped together. e.g. CBA will group Properties, then Functions, then Subs. Specifying -sAAA will sort all methods by name with no grouping.

-d

Displays the sorted file, but doesn't change the original.

-r

Restores the original file after sorting. When a file is sorted, a copy is made, this option will restore the file to the original. Note that there is only one backup copy.

To set this up in Visual Studio, add three external tools from Tools>External Tools.

Adding External Tools

Command: path to sort Method Sorting.exe.

Arguments for sorting:

-f$(ItemPath}" -sAAB

Arguments for restoring:

-f"$(ItemPath)" -r

Arguments to display only:

-f"$(ItemPath)" -d -sAAB

This will add a menu item to the Tools Menu.

When you sort the file, Visual Studio will detect that it has been changed externally and prompt you reload it, click Yes to load the sorted file. Note: This clears all previous Undo actions.

Points of Interest

There is no template or settings for this utility and it should work with most VB code, it will ignore all Structures, Interfaces and Declares that can contain methods, including DllImport directives.

It has been tested on a wide variety of native and interop code, but not with #Regions.

I haven't allowed for #Regions because I don't use them, but also, I have assumed (maybe incorrectly) that if you use #Regions you already have your code grouped and sorted as you want it to be.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Aimco Software
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI like this tool Pin
dthrun15-Sep-15 11:39
dthrun15-Sep-15 11:39 
Good tool. Is there anyway to stop adding the Sorting comments to the code? Can we see the source code for this? Thanks for sharing this.
AnswerRe: I like this tool Pin
UKAndrewC20-Sep-15 23:31
UKAndrewC20-Sep-15 23:31 
QuestionFor the second time today... Pin
OriginalGriff16-Jul-15 21:53
mveOriginalGriff16-Jul-15 21:53 
AnswerRe: For the second time today... Pin
UKAndrewC16-Jul-15 23:01
UKAndrewC16-Jul-15 23:01 
GeneralRe: For the second time today... Pin
OriginalGriff16-Jul-15 23:36
mveOriginalGriff16-Jul-15 23:36 
GeneralRe: For the second time today... Pin
UKAndrewC16-Jul-15 23:59
UKAndrewC16-Jul-15 23:59 
GeneralRe: For the second time today... Pin
OriginalGriff17-Jul-15 0:09
mveOriginalGriff17-Jul-15 0:09 
GeneralRe: For the second time today... Pin
UKAndrewC17-Jul-15 0:24
UKAndrewC17-Jul-15 0:24 
GeneralRe: For the second time today... Pin
Simon_Whale17-Jul-15 1:01
Simon_Whale17-Jul-15 1:01 
GeneralRe: For the second time today... Pin
UKAndrewC17-Jul-15 2:18
UKAndrewC17-Jul-15 2:18 
GeneralRe: For the second time today... Pin
Simon_Whale17-Jul-15 2:22
Simon_Whale17-Jul-15 2:22 
GeneralRe: For the second time today... Pin
UKAndrewC17-Jul-15 5:11
UKAndrewC17-Jul-15 5:11 
GeneralRe: For the second time today... Pin
Nelek20-Sep-15 23:39
protectorNelek20-Sep-15 23:39 
GeneralRe: For the second time today... Pin
UKAndrewC20-Sep-15 23:52
UKAndrewC20-Sep-15 23:52 
GeneralRe: For the second time today... Pin
Nelek21-Sep-15 1:12
protectorNelek21-Sep-15 1:12 
GeneralRe: For the second time today... Pin
UKAndrewC21-Sep-15 1:47
UKAndrewC21-Sep-15 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.