Comments and Style
Prerequisites: Subs and functions
Comments
Module Program
' A comment on its own line
Const TaxRate As Decimal = 0.2D ' a comment after code
REM An old-style comment (you'll see it in legacy code)
''' <summary>Returns the price including tax.</summary>
''' <param name="price">The net price.</param>
Function WithTax(price As Decimal) As Decimal
Return price * (1 + TaxRate)
End Function
Sub Main()
Console.WriteLine(WithTax(100D))
End Sub
End Module
120.0
'starts a comment that runs to the end of the line. VB has no block comments — select lines in Visual Studio and press Ctrl + K, Ctrl + C to comment them all.'''starts an XML documentation comment: Visual Studio shows it in IntelliSense tooltips.
Style guidelines
Microsoft's Visual Basic coding conventions recommend:
Option Strict Onin every project.PascalCasefor procedures, properties, classes, modules, and constants;camelCasefor local variables and parameters.- Meaningful names (
customerCount, notcc), and no type prefixes likestrNameorintCountfrom VB6 days. AndAlso/OrElsein conditions;&for joining strings.- One statement per line; indentation of four spaces (Visual Studio formats it for you, and "pretty listing" fixes keyword capitalization as you type).
Try/Catchinstead ofOn Error GoTo(Level 2).- Small procedures that do one thing, with early
Returns instead of deep nesting.
Tools
- Visual Studio — formatting, refactorings, code analysis, and the debugger.
dotnet format— applies formatting and code-style rules from the command line or CI.- .NET analyzers — built into the SDK; they flag common bugs and style issues during the build.
What's next
You now know the core of Visual Basic. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with classes and objects.