Line Feeds in VB

Sometimes it's the simple things that stump you. I'm converting somecode from C# to VB. What followed was a bit ofhead scratchingover what appeared to be the most trivial of tasks.

Take the following line and convert it to VB...

[C#]
System.Console.WriteLine("Hello\nWorld");

becomes...

[VB]
System.Console.WriteLine("Hello" & vbCrLf & "World")

or does it?

The VB code on my system produces "HelloWorld" rather than the two words on separate lines as expected.

Being a VB newbie, I have found the reason why to be twofold. Firstly, vbCrLf is not intrinsic to the language but rather defined as with other VB constants in the Microsoft.VisualBasic assembly. VS automagically imports the namespaces without telling anyone - the rest of us aren't so lucky. And secondly, because I am using a "scripting" product, explicit type checking is turned off by default - so vbCrLf is interpreted as an object rather than a constant.

The two combine to annoyingly require that users be instructed that they must explicitly import the Microsoft.VisualBasic namespace/s or take great care in the absence of explicit type checking.

Update: see here for another perspective on this issue... http://www.angrycoder.com/blog/entries/20030423.html

9 Comments

  • You should also note that the two arn't equivalent. The equivalent of vbCrLf is "\r\n", not "\n". You might get tripped up with this when playing with multiline TextBoxes - I did :)

  • Another thing to consider is System.Environment.NewLine. It's the CLR way and not language dependent. Just learned about that one last week, as I have been using vbCRLF for many many years!!

  • 1) This sounds like something that could dangerously turn into a "language war"... which will cause yourself and all others on .NETWebLogs who participate in this discussion to get deleted... so be very, very, very careful.





    2) First rule of VB.NET... Add Option Strict On to the top of your code; or turn it on project wide. Yes, by default it is turned off in order to act more like traditional VB; but I recommend always turning it on... This will enable strong type checking and also require variables to be declared.





    3) I'm not sure what you mean by "scripting" product... VB.NET is compiled to the exact same framework in the exact same way as C#. Don't make statements like this about something that you have even expressed yourself that you don't know about. It's just different, not any better or worse than C#.





    4) You are also being hit by the default way that VB requires variables being declared. By default Option Explicit is turned off. Again, add Option Strict On or Option Explicit On if you don't want strong type checking.





    5) Instead of vbCRLF, use vbLF. I think \n is a line feed. If it is a carriage return, then use vbCR.





    6) Also, if I'm not mistaken, Format() for strings will allow you to use escape characters as well.

  • I agree with Julie. Environment.NewLine is a language-agnostic way of accomplishing the same thing, and you never have to question the result.

  • Also, you can also use Chr(10) for LF and Chr(13) for CR.





    I just looked up Format... it doesn't support this. I swear I saw something in the Framework that accepts escape characters inside of a string. For the life of me, I can't remember where I saw it. It's wasn't a language specific thing; but rather some method inside the FCL that accepted these characters.





    You could also do the following:





    Function EscapeString(ByVal s As String) As String


    If s.IndexOf("\n") > 0 Then


    s = s.Replace("\n", Environment.NewLine)


    'or s = s.Replace("\n", vbLf)


    'or s = s.Replace("\n", Chr(10))


    End If


    If s.IndexOf("\r") > 0 Then


    s = s.Replace("\r", vbCr)


    'or s = s.Replace("\n", Chr(13))


    End If


    Return s


    End Function

  • Or for a more complete version:





    Function EscapeString(ByVal s As String) As String





      '\' - Single quote 0x0027


      If s.IndexOf("\'") > 0 Then


       s = s.Replace("\'", "'")


      End If





      '\" - Double quote 0x0022


      If s.IndexOf("\" & Chr(&H22)) Then


       s = s.Replace("\" & Chr(&H22), Chr(&H22))


      End If





      '\0 - Null 0x0000


      If s.IndexOf("\0") Then


       s = s.Replace("\0", Chr(&H0))


      End If





      '\a - Alert 0x0007


      If s.IndexOf("\a") > 0 Then


       s = s.Replace("\a", Chr(&H7))


      End If





      '\b - Backspace 0x0008


      If s.IndexOf("\b") > 0 Then


       s = s.Replace("\b", Chr(&H8))


      End If





      '\f - Form feed 0x000C


      If s.IndexOf("\f") > 0 Then


       s = s.Replace("\f", Chr(&HC))


      End If





      '\n - New line 0x000A


      If s.IndexOf("\n") > 0 Then


       s = s.Replace("\n", Environment.NewLine)


       'or s = s.Replace("\n", vbLf)


       'or s = s.Replace("\n", Chr(10))


       'or s = s.Replace("\n", Chr(&HA))


      End If





      '\r - Carriage return 0x000D


      If s.IndexOf("\r") > 0 Then


      

  • trying this again:





    Function EscapeString(ByVal s As String) As String


    '\' - Single quote 0x0027


    If s.IndexOf("\'") > 0 Then


    s = s.Replace("\'", "'")


    End If


    '\" - Double quote 0x0022


    If s.IndexOf("\" & Chr(&H22)) Then


    s = s.Replace("\" & Chr(&H22), Chr(&H22))


    End If


    '\0 - Null 0x0000


    If s.IndexOf("\0") Then


    s = s.Replace("\0", Chr(&H0))


    End If


    '\a - Alert 0x0007


    If s.IndexOf("\a") > 0 Then


    s = s.Replace("\a", Chr(&H7))


    End If


    '\b - Backspace 0x0008


    If s.IndexOf("\b") > 0 Then


    s = s.Replace("\b", Chr(&H8))


    End If


    '\f - Form feed 0x000C


    If s.IndexOf("\f") > 0 Then


    s = s.Replace("\f", Chr(&HC))


    End If


    '\n - New line 0x000A


    If s.IndexOf("\n") > 0 Then


    s = s.Replace("\n", Environment.NewLine)


    'or s = s.Replace("\n", vbLf)


    'or s = s.Replace("\n", Chr(10))


    'or s = s.Replace("\n", Chr(&HA))


    End If


    '\r - Carriage return 0x000D


    If s.IndexOf("\r") > 0 Then


    s = s.Replace("\r", vbCr)


    'or s = s.Replace("\n", Chr(13))


    'or s = s.Replace("\n", Chr(&HD))


    End If


    '\t - Horizontal tab 0x0009


    If s.IndexOf("\v") Then


    s = s.Replace("\v", Chr(&H9))


    End If


    '\v - Vertical tab 0x000B


    If s.IndexOf("\v") Then


    s = s.Replace("\v", Chr(&HB))


    End If


    '\\ - Backslash 0x005C


    If s.IndexOf("\\") Then


    s = s.Replace("\\", Chr(&H5C))

  • I'm not sure what you are referring to here. Are you trying to write this in VB.net insode a c# application? Id so, then you right, you have to import the Microsoft.VisualBasic Namespace.


    However if you are trying to do this in a VB.net project it worked just fine for me. I typed it exactly as you did and "Hello" appeared on the first line and "World" appeared on the second.

  • Three quick notes -





    1) Visual Studio.NET imports Microsoft.VisualBasic by default; I think the command-line compiler doesn't (though the dll is referenced by default).





    2) I thought Option Explicit was on by default in VB.NET; it's certainly on by default in Visaul Studio.

Comments have been disabled for this content.