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) " & _ " )"
The underscore is the line continuation character. It allows the concatenation to include a different line. Like so: x = "Hello " & "World" x = "Hello " & _ "World" 'this won't compile (pre vb.net 2010, anyway) x = "Hello " & "World"
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.
To continue a statement from one line to the next, type a space followed by the line-continuation character [the underscore character on your keyboard (_)].
No, you have to use the underscore, but I believe that VB.NET 10 will allow multiple lines w/o the underscore, only requiring if it can't figure out where the end should be.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With