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 es
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 & for ampersand, < 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>"
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
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
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.
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.