Line continuation


A single _ (underscore) character at the end of a line of code tells the compiler that the line continues in the next line. This allows a single statement (line of code) to be spread across multiple lines in the input file, which can be a nice formatting help.

'' This Dim statement is spread across multiple lines, using the '_' character
Dim myvariable _
As Integer


This is often used to make very long lines of code easier to read, for example procedure declarations with a lot of parameters:

'' Here's an example:

Declare Sub drawRectangle( ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer )

'' which can also be written as:

Declare Sub drawRectangle( ByVal x As Integer, ByVal y As Integer, _
                           ByVal w As Integer, ByVal h As Integer )

'' or:

Declare Sub drawRectangle _
    ( _
        ByVal x As Integer, _
        ByVal y As Integer, _
        ByVal w As Integer, _
        ByVal h As Integer _
    )

'' (or any other formatting you like)


The _ line continuation character can be inserted at pretty much any point in a line of code. It does not work inside comments though.

Be careful when adding the _ line continuation character right behind an identifier or keyword. It should be separated with at least one space character, otherwise it would be treated as part of the identifier or keyword.

'' Declare variable "a_"
'' (no line continuation happening, because the '_' character is part of
'' the "a_" identifier)
Dim As Integer a_

'' Declare variable "a" and initialize to value 5
'' (line continuation happening, because the '_' character
'' was separated from the identifier "a" with a space character)
Dim As Integer a _
= 5


Warning: When an erroneous code line is spread over a multiple lines block by using the _ line continuation character, the error message refers only to the last line of the block.

Back to Programmer's Guide
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode