Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

VB string question

Back to Forum: Tech Off
  • User profile image
    dotnetjunkie

    When dealing with multi-line strings, C# has a very elegant solution

    String s = @"line1
    line2
    line3
    line4"

    However in VB.NET you have to do
    Dim s as String = "line1" & vbNewLine & _
    "line2" & vbNewLine & _
    "line3" & vbNewLine & _
    "line4"

    Now, I was wondering: does this cause string concatenation in VB.NET?  Or is the compiler smart enough to make this into 1 string, so that the performance is exactly the same as the C# version?

    Actually, how do you VB.NET guys deal with chunks of text like that? (for example, a html or javascript fragment)  All the line breaking, changes to double quotes etc... make it a hell of a job to integrate such a snippet in your code!  Is there an easier alternative?

  • User profile image
    aghiondea

    How about string.Format() ? Wouldn't that be what you need?

    An regarding the question about the _ in the VB.NET code. I think that the compiler translates that into a single string, no perf issues there! And the same for C#.

  • User profile image
    dotnetjunkie

    No, I'm sorry, string.format has nothing to do with the problem (or more like inconvenience) I described.

    Maybe Sven Groot can post here?, I know that he's using VB.NET.

  • User profile image
    borosen

    dotnetjunkie wrote:
    

    Now, I was wondering: does this cause string concatenation in VB.NET?  Or is the compiler smart enough to make this into 1 string, so that the performance is exactly the same as the C# version?

    ildasm is your friend in cases like this.

    The answer is, the compiles is smart enough to make it into 1 string.

  • User profile image
    Sven Groot

    dotnetjunkie wrote:
    Maybe Sven Groot can post here?, I know that he's using VB.NET.

    As far as I know, there is no better way to write multiline strings in VB. Personally, I wouldn't use the old-style vbNewLine constant but the new-style ControlChars.NewLine (or Environment.NewLine) constant. Which, I know, is even longer still. Smiley

    Yes, the compiler will turn it into a single string constant. Pretty much any half-way decent compiler will pre-compute constant expressions during compilation.

    Ildasm/Reflector are certainly good ways to find the answer to questions such as that, but there's one catch. The VB and C# compiler do very little in terms of optimization, most of that work is left to the JIT compiler. And those tools can't tell you code the JIT will generate. In this case, the VB compiler already took care of the string constant though.

  • User profile image
    Foxfire

    dotnetjunkie wrote:
    

    When dealing with multi-line strings, C# has a very elegant solution

    String s = @"line1
    line2
    line3
    line4"

    However in VB.NET you have to do
    Dim s as String = "line1" & vbNewLine & _
    "line2" & vbNewLine & _
    "line3" & vbNewLine & _
    "line4"

    Now, I was wondering: does this cause string concatenation in VB.NET?  Or is the compiler smart enough to make this into 1 string, so that the performance is exactly the same as the C# version?

    Actually, how do you VB.NET guys deal with chunks of text like that? (for example, a html or javascript fragment)  All the line breaking, changes to double quotes etc... make it a hell of a job to integrate such a snippet in your code!  Is there an easier alternative?



    Well if you have strings containing Linebreaks or something similar the best option is probably to put them into external resources (e.g. resource files) anyway because they are most likey display output.

  • User profile image
    figuerres

    Foxfire wrote:
    
    dotnetjunkie wrote: 

    When dealing with multi-line strings, C# has a very elegant solution

    String s = @"line1
    line2
    line3
    line4"

    However in VB.NET you have to do
    Dim s as String = "line1" & vbNewLine & _
    "line2" & vbNewLine & _
    "line3" & vbNewLine & _
    "line4"

    Now, I was wondering: does this cause string concatenation in VB.NET?  Or is the compiler smart enough to make this into 1 string, so that the performance is exactly the same as the C# version?

    Actually, how do you VB.NET guys deal with chunks of text like that? (for example, a html or javascript fragment)  All the line breaking, changes to double quotes etc... make it a hell of a job to integrate such a snippet in your code!  Is there an easier alternative?



    Well if you have strings containing Linebreaks or something similar the best option is probably to put them into external resources (e.g. resource files) anyway because they are most likey display output.


    this is one of the areas I think a lot of coders get lazy....
    (me to btw!)

    if we use resource tables for our text that users read then we are much closer to being able to make our apps work for users in other countries.

    I think I need to look at what the chnages are in my own coding to make that work -- I know that before .net it was a pain.

    it looks like with .net 2 and vs 2005 is't a lot simpler to do.

    seems like a good topic for some kind of poll and maby some 9 videos etc....

    using resources in the real world of development.
    Big Smile

  • User profile image
    Lee_Dale

    In cases where im concatenating strings or building large strings like building up html for instance I would use the StringBuilder class.  I'm not a fan of line continuation personally and try to avoid it but would be interesting to know how the compiler treats line continuation my guess is it just treats it like one long line of code (which is what it's supposed to do).

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.