How to: Break and Combine Statements in Code (Visual Basic)
When writing your code, you might at times create lengthy statements that necessitate horizontal scrolling in the Code Editor. Although this doesn't affect the way your code runs, it makes it difficult for you or anyone else to read the code as it appears on the monitor. In such cases, you should consider breaking the single long statement into several lines.
To break a single statement into multiple lines
Use the line-continuation character, which is an underscore (
_), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.
In some cases, if you omit the line-continuation character, the Visual Basic compiler will implicitly continue the statement on the next line of code. For a list of syntax elements for which you can omit the line-continuation character, see "Implicit Line Continuation" in Statements.
In the following example, the statement is broken into four lines with line-continuation characters terminating all but the last line.
cmd.CommandText = _ "SELECT * FROM Titles JOIN Publishers " _ & "ON Publishers.PubId = Titles.PubID " _ & "WHERE Publishers.State = 'CA'"
Using this sequence makes your code easier to read, both online and when printed.
The line-continuation character must be the last character on a line. You can't follow it with anything else on the same line.
Some limitations exist as to where you can use the line-continuation character; for example, you can't use it in the middle of an argument name. You can break an argument list with the line-continuation character, but the individual names of the arguments must remain intact.
You can't continue a comment by using a line-continuation character. The compiler doesn't examine the characters in a comment for special meaning. For a multiple-line comment, repeat the comment symbol (
') on each line.
Although placing each statement on a separate line is the recommended method, Visual Basic also allows you to place multiple statements on the same line.
To place multiple statements on the same line
Separate the statements with a colon (
:), as in the following example:
text1.Text = "Hello" : text1.BackColor = System.Drawing.Color.Red