Differences Between vbLf, vbCrLf & vbCr Constants

vb.netPrintingConstantsMsgbox

vb.net Problem Overview


I used constants like vbLf , vbCrLf & vbCr in a MsgBox; it produces same output in a MsgBox (Text "Hai" appears in a first paragraph and a word "Welcome" appears in a next Paragraph )

MsgBox("Hai" & vbLf & "Welcome")
MsgBox ("Hai" & vbCrLf & "Welcome")
MsgBox("Hai" & vbCr & "Welcome")

I know vbLf , vbCrLf & vbCr are used for print and display functions.

I want to know the Difference between the vbLf , vbCrLf & vbCr constants.

vb.net Solutions


Solution 1 - vb.net

 Constant   Value	            Description
 ----------------------------------------------------------------
 vbCr	    Chr(13)	            Carriage return
 vbCrLf	    Chr(13) & Chr(10)   Carriage return–linefeed combination
 vbLf	    Chr(10)	            Line feed
  • vbCr : - return to line beginning Represents a carriage-return character for print and display functions.

  • vbCrLf : - similar to pressing Enter Represents a carriage-return character combined with a linefeed character for print and display functions.

  • vbLf : - go to next line Represents a linefeed character for print and display functions.


Read More from Constants Class

Solution 2 - vb.net

The three constants have similar functions nowadays, but different historical origins, and very occasionally you may be required to use one or the other.

You need to think back to the days of old manual typewriters to get the origins of this. There are two distinct actions needed to start a new line of text:

  1. move the typing head back to the left. In practice in a typewriter this is done by moving the roll which carries the paper (the "carriage") all the way back to the right -- the typing head is fixed. This is a carriage return.
  2. move the paper up by the width of one line. This is a line feed.

In computers, these two actions are represented by two different characters - carriage return is CR, ASCII character 13, vbCr; line feed is LF, ASCII character 10, vbLf. In the old days of teletypes and line printers, the printer needed to be sent these two characters -- traditionally in the sequence CRLF -- to start a new line, and so the CRLF combination -- vbCrLf -- became a traditional line ending sequence, in some computing environments.

The problem was, of course, that it made just as much sense to only use one character to mark the line ending, and have the terminal or printer perform both the carriage return and line feed actions automatically. And so before you knew it, we had 3 different valid line endings: LF alone (used in Unix and Macintoshes), CR alone (apparently used in older Mac OSes) and the CRLF combination (used in DOS, and hence in Windows). This in turn led to the complications of DOS / Windows programs having the option of opening files in text mode, where any CRLF pair read from the file was converted to a single CR (and vice versa when writing).

So - to cut a (much too) long story short - there are historical reasons for the existence of the three separate line separators, which are now often irrelevant: and perhaps the best course of action in .NET is to use Environment.NewLine which means someone else has decided for you which to use, and future portability issues should be reduced.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLawranceView Question on Stackoverflow
Solution 1 - vb.netVivek S.View Answer on Stackoverflow
Solution 2 - vb.netAATView Answer on Stackoverflow