PDA

Click to See Complete Forum and Search --> : Line cannot exceed 2046 characters


BrightSoul
Jun 27th, 2005, 12:01 PM
hello there,
I'm relatively new to C#. I have to define a veeeeery long constant string and when I try to run an aspx page (Framework 1.1) containing this line of code:

private const string longString = "believe me this is very long [...]";


I get a compilation error CS1034
"Compiler limit exceeded: Line cannot exceed 2046 characters"

Now, the MSDN isn't helping, it's just displaying this (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscomp/html/vcerrCompilerErrorSC1034.asp)

Do you know if there is any workaround to make it compile besides splitting the string into n pieces for subsequent concats into a variable? I'd really like to use a constant, this limitation is just silly if you ask me, expecially becuase if I had used VB.net I would have not encountered this problem.

Also, do you know if C# can use its own version of that useful Vb.net underscore feature that make long lines more readable?

Const longString As String = "abcdefg" & _
"hijklmnopq" & _
"rstuvwxyz012...."

Many thanks in advance :wave:

axion_sa
Jun 27th, 2005, 12:27 PM
I don't see what's wrong with the limit? It could get rather interesting trying to maintain that line...
In any case - C# doesn't have an underscore as it considers the semi-colon the EOL, and not the carriage return as in VB.

const string LongString = "abcdefg"
+ "hijklmno"
+ "qrstuvw";

BrightSoul
Jun 27th, 2005, 12:38 PM
:blush:

It was easier than expected, yet I couldn't figure it out.
it works, thanks :thumb:

hellswraith
Jun 27th, 2005, 11:54 PM
You can do this with C# (pretty cool for sql statements):


string myString = @"
SELECT *
FROM myTable
WHERE ID = 5";

GlenW
Jun 28th, 2005, 09:27 AM
If you're going to be manipulating a string a lot it is much better to use a StringBuilder.

string lstrString = "qwerty";
string lstrString2 = lstrString + "asdfg";

Will actually end up with 5 strings in memory; lstrString, lstrString2, "qwerty", "asdfg" and another lstrString.

jmcilhinney
Jun 28th, 2005, 09:45 AM
If you're going to be manipulating a string a lot it is much better to use a StringBuilder.

string lstrString = "qwerty";
string lstrString2 = lstrString + "asdfg";

Will actually end up with 5 strings in memory; lstrString, lstrString2, "qwerty", "asdfg" and another lstrString.You are correct about the StringBuilder but not quite about the number of String objects. Here are the events:

1. Create String reference (lstrString) on the stack.
2. Create String object ("qwerty") on the heap.
3. Set reference in 1 to object in 2.
4. Create String reference (lstrString2) on the stack.
5. Create String object ("asdfg") on the heap.
6. Create String object (lstrString + "asdfg") on the heap.
7. Set reference in 4 to object in 6.

This equals 2 references but only 3 objects. In this situation a StringBuilder would not save you much, if anything. However in situations we've all seen where an SQL statement is built up by concatenating the contents of 10 TextBoxes with literals in between, each concatenation is another String object on the heap so a StringBuilder would be much more efficient.