The Print Method – Part 1

Using the Semicolon (;) Separator

 

The Print  Method

 

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 of the following objects:

 

·         Form                      You can print lines of data directly to the background of a form.  This is done in the examples in the section, although it is not typically done in regular VB applications. Statements that print to a form begin in any of the following ways:

 

                                                            Form1.Print . . .

Me.Print . . .

Print . . .

 

·         Picture Box            You can print lines of data or graphics to a  PictureBox control, with syntax beginning like the following:

 

PictureBox1.Print . . .

 

·         Debug object         You can print lines of data to the VB Immediate Window, using syntax beginning like the following:

 

Debug.Print . . .

 

·         Printer                   You can print lines of data to the Windows default printer, using syntax beginning like the following:

 

Printer.Print . . .

 

 

To write data to a file, the Print # statement is used, using syntax beginning like the following:

 

Print #1, . . .

 

Printing to files is a common way to write data to a sequential file; this will be covered in detail in a later section.

 

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".

 

Printing with the semicolon (;) separator

 

As noted above, 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?

 

As you can see, there is a slight problem which is easily fixed.  But first, make sure you understand that the Print statement above contains three distinct items in its expression list:  the string constant "Hello,", the string variable strName (whose contents will be printed when the Print statement is executed), and the string constant "How are you today?".  Note that each item was printed immediately one after the other.

 

The problem is fixed by adding a blank space to the end of the first string constant (within the quotes) and adding a period and a blank space to the beginning of the last string constant (within the quotes):

 

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

 

This fixed Print statement would generate better looking output:

 

      Hello, HARRY. How are you today?

 

Now consider another program segment:

 

      intSum = intFirstNum + intSecondNum

      Print "The sum of"; intFirstNum; "and"; intSecondNum; "is"; intSum

 

Assuming all the variables involved were numeric, and that intFirstNum contained 3 and intSecondNum contained 2,  the Print statement would generate:

 

            The sum of 3 and 2 is 5

 

Note that there are six distinct items in the Print statement's expression list:  the string constant "The sum of", the content of the variable intFirstNum, the string constant "and", the content of the variable intSecondNum, the string constant "is", and the content of the variable intSum.

 

Note that it was not necessary to place extra spaces in the string constants of the Print statement this time.  That is because, by default,  VB always outputs numeric items with a leading sign and a trailing space.  (If the sign of the number is positive, a blank space will print; if negative, a "-" will print.)

 

If a Print statement ends with a semicolon, any item printed by a subsequent Print statement will be printed on the same line, immediately following the last item that was printed.  In effect, a semicolon at the end of a Print statement suppresses the carriage return/line feed to the next line.

 

Examples:

 

Print Statements

Output

Print "Hello";

Print "There"

 

HelloThere

For intX = 1 To 10

    Print intX;

Next

 

 

1  2  3  4  5  6  7  8  9  10

For intX = 1 To 10

    Print "*";

Next

 

 

**********

 

Note: For/Next and other looping constructs will be discussed in a later topic.