Skip to content
AgileLogics
4 min read

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 On in every project.
  • PascalCase for procedures, properties, classes, modules, and constants; camelCase for local variables and parameters.
  • Meaningful names (customerCount, not cc), and no type prefixes like strName or intCount from VB6 days.
  • AndAlso / OrElse in 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/Catch instead of On 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.