Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
photo of Dan Mabbutt

Dan's Visual Basic Blog

By Dan Mabbutt, About.com Guide to Visual Basic since 2002

NewLine - For All Platforms

Friday August 1, 2008

A lot of us code vbCrLf at the end of a string when we want to move to a new line in the output.

MsgBox( _
   "This is a message" _
   & NewLine & _
   "on two lines.")

The "CrLf" stands for "carriage return, line feed" and is compiled into hex 0D & 0A. It's been the way I've programmed it for ages. For Windows platforms, that's not a problem. But if there's a chance that your code will run on another platform, you probably want to use Environment.vbNewLine. There are .NET ports for non-Windows environments, such as Mono.

The reason is that for some of these other environments, CrLf isn't a "new line". The Microsoft doc states that:

"The property value of NewLine is a constant customized specifically for the current platform and implementation of the .NET Framework."

Note that Microsoft also provides a different NewLine in the namespace Microsoft.VisualBasic.ControlChars. This code ...


Imports _
Microsoft.VisualBasic.ControlChars
Public Class Form1
    Private Sub Button1_Click( ...
        MsgBox( _
           "This is a message" _
           & NewLine & _
           "on two lines.")
    End Sub
End Class

... will give you a CrLf and not a platform independant value. (But then, the Imports for Microsoft.VisualBasic.ControlChars would probably fail on another platform anyway.)

Comments

August 3, 2008 at 5:16 am
(1) Mike Sr says:

Dan,

While I don’t do cross platform, and I am still using VB6, I can appreciate this issue.

I created an Instrument Control thingy and added a button to allow the user to include a vbcrlf in a user created prompt string…

There was just one tiny problem with this.

Saving it was no problem… But…

I was using Line Input to read in a line at a time! (of the Procedure File) Opps…

Remedy? I added support for \n…

May your grins be big, very big, and long…

Mike Sr.

August 4, 2008 at 6:09 am
(2) Denzil says:

Dan,

I started using vbNewLine a while back (on VB6) because when developing a “StringBuilder” class I was told that for some inexplainable reason vbNewLine resolved slightly more quickly than vbCrLf. I never tested it myself, but took them at their word.

More to the point though, vbNewLine is more human readable – if you go for that sort of thing :)

Denzil

August 4, 2008 at 10:47 am
(3) Dan Mabbutt says:

I’m not sure about faster, but vbNewLine doesn’t get converted to a platform independant form. Only “Environment.NewLine” (when using the .NET Framework) does.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.