Visual Basic 2010: Documenting Source Code With XML Comments
Visual Basic 2010 supports XML documentation comments: structured comments that Visual Studio turns into IntelliSense tooltips and an XML file that documentation tools can process.
Adding a comment block
Place the cursor on the line above a class, method, property or field and type three apostrophes ('''). Visual Studio inserts a template:
''' <summary>
''' Calculates the gross price including VAT.
''' </summary>
''' <param name="net">The net price.</param>
''' <param name="rate">The VAT rate, for example 0.2 for 20%.</param>
''' <returns>The gross price.</returns>
''' <remarks>Rounds to two decimal places.</remarks>
Public Function GrossPrice(net As Decimal, rate As Decimal) As Decimal
Return Math.Round(net * (1 + rate), 2)
End Function
The most useful tags
| Tag | Purpose |
|---|---|
<summary> |
short description shown in IntelliSense |
<param name=""> |
describes a parameter |
<returns> |
describes the return value |
<remarks> |
longer notes |
<exception cref=""> |
exceptions the member can throw |
<example> and <code> |
usage examples |
<see cref=""> |
link to another type or member inline |
<seealso cref=""> |
"see also" links |
<value> |
describes a property value |
Use <paramref name="net"/> to refer to a parameter inside text and <c> for inline code.
Generating the XML file
In Project Properties > Compile, make sure Generate XML documentation file is ticked (it is on by default for VB projects). The build produces ProjectName.xml next to the assembly. When another project references the DLL with the XML file beside it, IntelliSense shows your summaries and parameter descriptions.
Visual Studio also warns about malformed XML or parameters that don't exist, which keeps documentation in sync with the code.
Producing documentation
Tools such as Sandcastle (and the Sandcastle Help File Builder) at the time turned the XML file into CHM or web help.
Tips
- Write the summary for the caller: what it does, not how.
- Document every public member of a library; private members are optional.
- Keep examples compilable.
Today's equivalent
The same ''' syntax and tags work in current Visual Basic and in C# (///). DocFX is the common tool for generating documentation websites from the XML files.