3

What is the effect of having a semicolon at the end of a line of code?

I've seen this in some code I've taken over:

Printer.Print "Customer: " & strCustomerName & " (" & strCustomerCode & ")";
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • The statement isn't even really "correct." The concatenation operators should have been semicolons too. Does *nobody* actually know VB??? – Bob77 Feb 15 '12 at 20:56
  • @BobRiemersma: No, `&` just combines the strings (with coercion if necessary), no need for semicolons in there. Then `Print` sees a single string (followed by the "no CRLF" thingy). – T.J. Crowder Feb 15 '12 at 21:56
  • Of course it does, but using concatenation is both slower and unnecessary in a Print. And it is not "no CRLF" but "no newline" which has an entirely different meaning, i.e. newlines as in vbNewLine are portable to Mac VBA. – Bob77 Feb 15 '12 at 23:02

2 Answers2

7

A ; at the end of a Print statement suppresses the usual default CRLF:

charpos - Specifies the insertion point for the next character. Use a semicolon to position the insertion point immediately after the last character displayed. Use Tab(n) to position the insertion point to an absolute column number. Use Tab with no argument to position the insertion point at the beginning of the next print zone. If charpos is omitted, the next character is printed on the next line.

(My emphasis)

I can't find a reference for Printer.Print (it's not listed if you click the "Methods" link here), but I expect it does the same thing.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Ok, strange. Why not just have a `Print` and `PrintLine` to distinguish this behaviour? – CJ7 Feb 15 '12 at 09:39
  • 3
    @CraigJ: For whatever reason, classicly BASIC (and therefore VB6) tended to do things this way, where it would be line-oriented unless you told it otherwise. Probably historic reasons. Separately, though, if you're going to start asking why VB6 does things the way it does, you'll soon have little time for anything else. :-) – T.J. Crowder Feb 15 '12 at 10:02
  • +1. @CraigJ You seem to be asking a few of these "how does VB6 work" questions. No offence meant, but you might find it quicker to read the manual. Or you could learn about the differences between VB6 and VB.Net by reading [some](http://msdn.microsoft.com/en-us/library/skw8dhdd.aspx) of the material to help VB6 users learn VB.Net. Might help you deduce how VB6 works. Alternatively there's [other](http://stackoverflow.com/questions/1595737/good-tutorial-for-visual-basic-6) [questions](http://stackoverflow.com/questions/166138/learning-vb6) on learning VB6 for experienced programmers. – MarkJ Feb 15 '12 at 11:17
  • @CraigJ ... And +1 T. J. Crowder for advising that it will be quite time-consuming trying to find out why VB6 does things the way it does. If you really want someone to blame, try Joel Spolsky. [He says it was his fault](http://www.joelonsoftware.com/items/2009/03/09.html) :) – MarkJ Feb 15 '12 at 11:21
  • Print statements offer more than a simple Print/PrintLine could. The semicolon isn't merely used at the ends of Print statements, but within lists of items to be printed as well. It is a formatting effector, like the comma or the inline Tab() and Spc() functions. As stated in another answer, this is part of the syntax in true Basic dialects and goes back decades. – Bob77 Feb 15 '12 at 20:45
1

Print is a fundamental BASIC statement that dates back to the first days of the language in the mid-1960s. Print is used to display lines of data on a form, picture box, printer, and the immediate (Debug) window; it can also be used to write records of data to a file. In VB, Print is implemented as a method.

The general format for the Print method is:

[object.]Print [expressionlist]

where object refers to one of the objects mentioned above (Form, PictureBox, Debug window, Printer) and expressionlist refers to a list of one or more numeric or string expressions to print.

Items in the expression list may be separated with semicolons (;) or commas (,). A semicolon or comma in the expression list determines where the next output begins:

; (semicolon) means print immediately after the last value.
, (comma) means print at the start of the next "print zone".

Items in the expression list of a Print statement that are separated by semicolons print immediately after one another. In the statement

Print "Hello,"; strName; "How are you today?"

If strName contained "HARRY", the Print statement would generate the following output:

Hello,HARRYHow are you today?

Excerpt : Understanding semicolons and print method

Frankline
  • 40,277
  • 8
  • 44
  • 75
  • Actually it is more of a pseudo-method that the compiler handles directly. This is why it does not work within a With-block, etc. – Bob77 Feb 15 '12 at 20:37