114

I was wondering if there is a way (hopefully keyboard shortcut) to create auto generate function headers in visual studio.

Example:

Private Function Foo(ByVal param1 As String, ByVal param2 As Integer)

And it would automagically become something like this...


'---------------------------------- 
'Pre: 
'Post:
'Author: 
'Date: 
'Param1 (String): 
'Param2 (Integer): 
'Summary: 
Private Function Foo(ByVal param1 As String, ByVal param2 As Integer)
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ryan M
  • 1,721
  • 2
  • 15
  • 22
  • 1
    If you've landed on this page because this feature seems to be broken in your IDE you should ensure that your code compiles and try again. This feature doesn't work when your code has parsing errors. – krowe2 Jan 06 '17 at 22:49
  • How to generate todo list in xamarin? – Manthan Mar 28 '18 at 11:13

8 Answers8

193

Make that "three single comment-markers"

In C# it's ///

which as default spits out:

/// <summary>
/// 
/// </summary>
/// <returns></returns>

Here's some tips on editing VS templates.

Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
51

GhostDoc!

Right-click on the function, select "Document this" and

private bool FindTheFoo(int numberOfFoos)

becomes

/// <summary>
/// Finds the foo.
/// </summary>
/// <param name="numberOfFoos">The number of foos.</param>
/// <returns></returns>
private bool FindTheFoo(int numberOfFoos)

(yes, it is all autogenerated).

It has support for C#, VB.NET and C/C++. It is per default mapped to Ctrl+Shift+D.

Remember: you should add information beyond the method signature to the documentation. Don't just stop with the autogenerated documentation. The value of a tool like this is that it automatically generates the documentation that can be extracted from the method signature, so any information you add should be new information.

That being said, I personally prefer when methods are totally selfdocumenting, but sometimes you will have coding-standards that mandate outside documentation, and then a tool like this will save you a lot of braindead typing.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
  • 19
    And this is exactly the kind of 'documentation' that I detest. It just adds bytes without telling me anything the method and parameter names don't tell me already. Do not do this, without editing the comment into some worth-while... :-( – peSHIr Jan 12 '09 at 08:38
  • 15
    Of course you should be editing it to add information. But as a template it is very nice. – Rasmus Faber Jan 12 '09 at 09:27
  • 3
    @Rasmus: It's a template that, for good documentation, should be thrown away completely and rewritten anyway, since it has no informational content. So it's actually more effort than if it were just blank. – Joey Feb 23 '17 at 15:28
  • it not working on VISUAL STUDIO 2022 – zolfaghari Dec 07 '22 at 06:43
42
///

is the shortcut for getting the Method Description comment block. But make sure you have written the function name and signature before adding it. First write the Function name and signature.

Then above the function name just type ///

and you will get it automatically

enter image description here

Bimzee
  • 1,138
  • 12
  • 15
20

Visual Assist has a nice solution too, and is highly customizable.

After tweaking it to generate doxygen-style comments, these two clicks would produce -

/**
* Method:    FindTheFoo
* FullName:  FindTheFoo
* Access:    private 
* Qualifier:
* @param    int numberOfFoos
* @return   bool
*/
private bool FindTheFoo(int numberOfFoos)
{

}

(Under default settings, its a bit different.)


Edit: The way to customize the 'document method' text is under VassistX->Visual Assist Options->Suggestions, select 'Edit VA Snippets', Language: C++, Type: Refactoring, then go to 'Document Method' and customize. The above example is generated by:

va_doxy

To Insert The Snippet: with cursor in method name/signature, alt+shift+q > "document method"

OneAndOnly
  • 1,048
  • 1
  • 13
  • 33
Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
15

Normally, Visual Studio creates it automatically if you add three single comment-markers above the thing you like to comment (method, class).

In C# this would be ///.

If Visual Studio doesn't do this, you can enable it in

Options->Text Editor->C#->Advanced

and check

Generate XML documentation comments for ///

pictured description

n611x007
  • 8,952
  • 8
  • 59
  • 102
Domysee
  • 12,718
  • 10
  • 53
  • 84
5

In visual basic, if you create your function/sub first, then on the line above it, you type ' three times, it will auto-generate the relevant xml for documentation. This also shows up when you mouseover in intellisense, and when you are making use of the function.

Paul Ishak
  • 1,093
  • 14
  • 19
2

You can use code snippets to insert any lines you want.

Also, if you type three single quotation marks (''') on the line above the function header, it will insert the XML header template that you can then fill out.

These XML comments can be interpreted by documentation software, and they are included in the build output as an assembly.xml file. If you keep that XML file with the DLL and reference that DLL in another project, those comments become available in intellisense.

DCNYAM
  • 11,966
  • 8
  • 53
  • 70
-1

I'm working on an open-source project called Todoc which analyzes words to produce proper documentation output automatically when saving a file. It respects existing comments and is really fast and fluid.

http://todoc.codeplex.com/

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187