Line cannot exceed 2046 characters
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:
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
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?
Code:
Const longString As String = "abcdefg" & _
"hijklmnopq" & _
"rstuvwxyz012...."
Many thanks in advance :wave:
Re: Line cannot exceed 2046 characters
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.
Code:
const string LongString = "abcdefg"
+ "hijklmno"
+ "qrstuvw";
Re: Line cannot exceed 2046 characters
:blush:
It was easier than expected, yet I couldn't figure it out.
it works, thanks :thumb:
Re: Line cannot exceed 2046 characters
You can do this with C# (pretty cool for sql statements):
Code:
string myString = @"
SELECT *
FROM myTable
WHERE ID = 5";
Re: Line cannot exceed 2046 characters
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.
Re: Line cannot exceed 2046 characters
Quote:
Originally Posted by GlenW
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.