1

I have a string that is 109000 characters in length, and it wont fit on one line, I get a "line is too long." error.
I know that with normal CODE you can do the "_" at the end of your code like

    this code is too long so I will use the _
    this code acts like it is on the same line

but in a string it takes the "_" to be part of a string (as it should). There is no information that I could find on this, so here it is for you guys, stackoverflow.

user3659330
  • 13
  • 1
  • 3

2 Answers2

3

The documentation on the "Line is too long" error states that the maxiumum line length is 65535, so this is why you are getting the error.

There are a few solutions:

You can concatenate the string using the &

Dim s As String = "this code is too long so I will use the" &
                  "this code acts like it is on the same line"

Note you can also use + to concatenate string but make sure you have Option Strict On if you do (& is safer as the result is always a String). See comparison between the two here: Ampersand vs plus for concatenating strings in VB.NET

You can use a string builder. This may be more efficient if you are continually adding strings to the original string (especially if you do this in a loop):

Dim sb As new StringBuilder
sb.Append("this code is too long so I will use the")
sb.Append("this code acts like it is on the same line")

Debug.Writeline(sb.ToString)

See MSDN for More information about Concatenation here

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • For the top code shouldn't you use a "_" after the "&" to show you are moving to the next line? @Matt Wilko – user3659330 May 22 '14 at 02:08
  • @user3659330 - No. See the [VB coding conventions](http://msdn.microsoft.com/en-us/library/h63fsef3(v=vs.120).aspx) that state "Avoid using the explicit line continuation character "_" in favor of implicit line continuation wherever the language allows it." – Matt Wilko May 22 '14 at 07:56
-1
Dim longString As String = "loooooooooooooooooooooooooo" + _
"ooooooooooooooggggggg"
HengChin
  • 583
  • 5
  • 16