Results 1 to 6 of 6

Thread: Line cannot exceed 2046 characters

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89

    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
    Last edited by BrightSoul; Jun 27th, 2005 at 12:07 PM.
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89

    Re: Line cannot exceed 2046 characters



    It was easier than expected, yet I couldn't figure it out.
    it works, thanks
    Last edited by BrightSoul; Jun 27th, 2005 at 12:41 PM.
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

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

  5. #5
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width