Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single statement across multiple lines in VB.NET without the underscore character

Tags:

vb.net

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) " & _     "    )" 
like image 529
Shawn Avatar asked Jan 20 '09 15:01

Shawn


People also ask

What does & _ mean in VB?

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"

What is the use of multiline continuation character?

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.

How do I continue a string on the next line in VB net?

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 (_)].


2 Answers

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.

like image 128
Booji Boy Avatar answered Oct 09 '22 09:10

Booji Boy


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.

like image 36
Darren Avatar answered Oct 09 '22 08:10

Darren