tags:

views:

6678

answers:

7

Is there any 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, cant do this
              select a.articleName


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

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

Nathan Koop
+3  A: 

VB uses line break as the statement terminator. There is no way to change it (it's like asking C# not to expect ; at the end of statements). There is a solution however, you can always switch to C#!

Mehrdad Afshari
yea as soon as you call my boss and convince him that its some how profitable to fire/teach the other employees
Shawn Simon
+1  A: 

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 ;-)

ChrisA
alas, you lose intelisense.Also, tut, tut :-D
Nathan Koop
Actually, stringbuilder probably wouldn't help you in that code: it's all literals -no variables- so compiler would likely optimize the difference away.
Joel Coehoorn
+6  A: 

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.

Booji Boy
That's what the presenters said at the MSDN Developer's Conference in Minneapolis a couple weeks ago.
Bryan Anderson
Cool, I heard it on dotNetRocks I think.
Booji Boy
mmm interesting: http://blogs.msdn.com/vbteam/archive/2009/03/27/implicit-line-continuation-in-vb-10-tyler-whitney.aspx
Wilfred Knievel
+1 for mentioning VB10.
Moayad Mardini
A: 

You can't do that then the datatype of the phrase is a string and will not be compiled as a LINQ statement?

A: 

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>"

Hafthor