[vb.net] Single statement across multiple lines in VB.NET without the underscore character

Is there a way to have a statement across multiple lines without the underscore character?

It is really annoying in multi-line strings such as SQL statements, and especially LINQ queries.

Beside from the ugliness and difficulty when changing a line (nothing lines up anymore), you can't use comments in the middle of the multi-line statement.

Examples of my daily personal hell.

dim results = from a in articles _
              where a.articleID = 4 _ ' articleID     - Nope, can't do this
              select a.articleName


dim createArticle as string = _
    "Create table article " & _
    "    (articleID int " & _
    "    ,articleName varchar(50) " & _
    "    )"

This question is related to vb.net

The answer is


Not sure if you can do that with multi-line code, but multi-line variables can be done:

Multiline strings in VB.NET

Here is the relevant chunk:

I figured out how to use both <![CDATA[ along with <%= for variables, which allows you to code without worry.

You basically have to terminate the CDATA tags before the VB variable and then re-add it after so the CDATA does not capture the VB code. You need to wrap the entire code block in a tag because you will you have multiple CDATA blocks.

Dim script As String = <code><![CDATA[
  <script type="text/javascript">
    var URL = ']]><%= domain %><![CDATA[/mypage.html';
  </script>]]>
</code>.value

Starting with VB.NET 10, implicit line continuation is supported for a number of syntax elements (see Statements in Visual Basic: Continuing a Statement over Multiple Lines). The concatenation operator (&) is in the list of syntax elements, that support implicit line continuation. Interspersed comments are supported as well, so your second example could be rewritten as:

dim createArticle as string = 
    "Create table article " &
    "    (articleID int " &           ' Comment for articleID
    "    ,articleName varchar(50) " & ' Comment for articleName
    "    )"

Implicit line continuation also works for query operators, with a few restrictions:

Before and after query operators (Aggregate, Distinct, From, Group By, Group Join, Join, Let, Order By, Select, Skip, Skip While, Take, Take While, Where, In, Into, On, Ascending, and Descending). You cannot break a line between the keywords of query operators that are made up of multiple keywords (Order By, Group Join, Take While, and Skip While).

Again, interspersed comments are allowed, so your first example can be rewritten as:

dim results = from a in articles
              where a.articleID = 4  ' articleID - now perfectly legal
              select a.articleName

No, the underscore is the only continuation character. Personally I prefer the occasional use of a continuation character to being forced to use it always as in C#, but apart from the comments issue (which I'd agree is sometimes annoying), getting things to line up is not an issue.

With VS2008 at any rate, just select the second and following lines, hit the tab key several times, and it moves the whole lot across.

If it goes a tiny bit too far, you can delete the excess space a character at a time. It's a little fiddly, but it stays put once it's saved.

On the rare cases where this isn't good enough, I sometimes use the following technique to get it all to line up:

dim results as String = ""
results += "from a in articles "
results += "where a.articleID = 4 " 'and now you can add comments
results += "select a.articleName"

It's not perfect, and I know those that prefer C# will be tut-tutting, but there it is. It's a style choice, but I still prefer it to endless semi-colons.

Now I'm just waiting for someone to tell me I should have used a StringBuilder ;-)


I know that this has an accepted answer, but it is the top article I found when searching for this question, and it is very out of date now. I did a little more research, and this MSDN article contains a list of syntax elements that implicitly continue the statement on the next line of code for VB.Net 2010.


No, the underscore is the only continuation character. Personally I prefer the occasional use of a continuation character to being forced to use it always as in C#, but apart from the comments issue (which I'd agree is sometimes annoying), getting things to line up is not an issue.

With VS2008 at any rate, just select the second and following lines, hit the tab key several times, and it moves the whole lot across.

If it goes a tiny bit too far, you can delete the excess space a character at a time. It's a little fiddly, but it stays put once it's saved.

On the rare cases where this isn't good enough, I sometimes use the following technique to get it all to line up:

dim results as String = ""
results += "from a in articles "
results += "where a.articleID = 4 " 'and now you can add comments
results += "select a.articleName"

It's not perfect, and I know those that prefer C# will be tut-tutting, but there it is. It's a style choice, but I still prefer it to endless semi-colons.

Now I'm just waiting for someone to tell me I should have used a StringBuilder ;-)


For most multiline strings using an XML element with an inner CDATA block is easier to avoid having to escape anything for simple raw string data.

Dim s as string = <s><![CDATA[Line 1
line 2
line 3]]></s>.Value

Note that I've seen many people state the same format but without the wrapping "< s >" tag (just the CDATA block) but visual studio Automatic formatting seams to alter the leading whitespace of each line then. I think this is due to the object inheritance structure behind the Linq "X" objects. CDATA is not a "Container", the outer block is an XElement which inherits from XContainer.


No, the underscore is the only continuation character. Personally I prefer the occasional use of a continuation character to being forced to use it always as in C#, but apart from the comments issue (which I'd agree is sometimes annoying), getting things to line up is not an issue.

With VS2008 at any rate, just select the second and following lines, hit the tab key several times, and it moves the whole lot across.

If it goes a tiny bit too far, you can delete the excess space a character at a time. It's a little fiddly, but it stays put once it's saved.

On the rare cases where this isn't good enough, I sometimes use the following technique to get it all to line up:

dim results as String = ""
results += "from a in articles "
results += "where a.articleID = 4 " 'and now you can add comments
results += "select a.articleName"

It's not perfect, and I know those that prefer C# will be tut-tutting, but there it is. It's a style choice, but I still prefer it to endless semi-colons.

Now I'm just waiting for someone to tell me I should have used a StringBuilder ;-)


Not sure if you can do that with multi-line code, but multi-line variables can be done:

Multiline strings in VB.NET

Here is the relevant chunk:

I figured out how to use both <![CDATA[ along with <%= for variables, which allows you to code without worry.

You basically have to terminate the CDATA tags before the VB variable and then re-add it after so the CDATA does not capture the VB code. You need to wrap the entire code block in a tag because you will you have multiple CDATA blocks.

Dim script As String = <code><![CDATA[
  <script type="text/javascript">
    var URL = ']]><%= domain %><![CDATA[/mypage.html';
  </script>]]>
</code>.value

I will say no.

But the only proof that I have is personal experience and the fact that documentation on line continuation doesn't have anything else in it.

http://msdn.microsoft.com/en-us/library/aa711641(VS.71).aspx


I will say no.

But the only proof that I have is personal experience and the fact that documentation on line continuation doesn't have anything else in it.

http://msdn.microsoft.com/en-us/library/aa711641(VS.71).aspx


I know that this has an accepted answer, but it is the top article I found when searching for this question, and it is very out of date now. I did a little more research, and this MSDN article contains a list of syntax elements that implicitly continue the statement on the next line of code for VB.Net 2010.


Starting with VB.NET 10, implicit line continuation is supported for a number of syntax elements (see Statements in Visual Basic: Continuing a Statement over Multiple Lines). The concatenation operator (&) is in the list of syntax elements, that support implicit line continuation. Interspersed comments are supported as well, so your second example could be rewritten as:

dim createArticle as string = 
    "Create table article " &
    "    (articleID int " &           ' Comment for articleID
    "    ,articleName varchar(50) " & ' Comment for articleName
    "    )"

Implicit line continuation also works for query operators, with a few restrictions:

Before and after query operators (Aggregate, Distinct, From, Group By, Group Join, Join, Let, Order By, Select, Skip, Skip While, Take, Take While, Where, In, Into, On, Ascending, and Descending). You cannot break a line between the keywords of query operators that are made up of multiple keywords (Order By, Group Join, Take While, and Skip While).

Again, interspersed comments are allowed, so your first example can be rewritten as:

dim results = from a in articles
              where a.articleID = 4  ' articleID - now perfectly legal
              select a.articleName

For most multiline strings using an XML element with an inner CDATA block is easier to avoid having to escape anything for simple raw string data.

Dim s as string = <s><![CDATA[Line 1
line 2
line 3]]></s>.Value

Note that I've seen many people state the same format but without the wrapping "< s >" tag (just the CDATA block) but visual studio Automatic formatting seams to alter the leading whitespace of each line then. I think this is due to the object inheritance structure behind the Linq "X" objects. CDATA is not a "Container", the outer block is an XElement which inherits from XContainer.


I will say no.

But the only proof that I have is personal experience and the fact that documentation on line continuation doesn't have anything else in it.

http://msdn.microsoft.com/en-us/library/aa711641(VS.71).aspx


You can use XML literals to sort of do this:

Dim s = <sql>
  Create table article
  (articleID int  -- sql comment
  ,articleName varchar(50)  <comment text="here's an xml comment" />
  )
</sql>.Value

warning: XML text rules apply, like &amp; for ampersand, &lt; for <, etc.

Dim alertText = "Hello World"
Dim js = <script>
          $(document).ready(function() {
           alert('<%= alertText %>');
          });
         </script>.ToString  'instead of .Value includes <script> tag

note: the above is problematic when it includes characters that need to be escaped. Better to use "<script>"+js.Value+"</script>"


No, the underscore is the only continuation character. Personally I prefer the occasional use of a continuation character to being forced to use it always as in C#, but apart from the comments issue (which I'd agree is sometimes annoying), getting things to line up is not an issue.

With VS2008 at any rate, just select the second and following lines, hit the tab key several times, and it moves the whole lot across.

If it goes a tiny bit too far, you can delete the excess space a character at a time. It's a little fiddly, but it stays put once it's saved.

On the rare cases where this isn't good enough, I sometimes use the following technique to get it all to line up:

dim results as String = ""
results += "from a in articles "
results += "where a.articleID = 4 " 'and now you can add comments
results += "select a.articleName"

It's not perfect, and I know those that prefer C# will be tut-tutting, but there it is. It's a style choice, but I still prefer it to endless semi-colons.

Now I'm just waiting for someone to tell me I should have used a StringBuilder ;-)


You can use XML literals to sort of do this:

Dim s = <sql>
  Create table article
  (articleID int  -- sql comment
  ,articleName varchar(50)  <comment text="here's an xml comment" />
  )
</sql>.Value

warning: XML text rules apply, like &amp; for ampersand, &lt; for <, etc.

Dim alertText = "Hello World"
Dim js = <script>
          $(document).ready(function() {
           alert('<%= alertText %>');
          });
         </script>.ToString  'instead of .Value includes <script> tag

note: the above is problematic when it includes characters that need to be escaped. Better to use "<script>"+js.Value+"</script>"


I will say no.

But the only proof that I have is personal experience and the fact that documentation on line continuation doesn't have anything else in it.

http://msdn.microsoft.com/en-us/library/aa711641(VS.71).aspx