Understanding semicolans and the print method

Level:
Level1

Written By TheVBProgramer.

$ReqTestHarness$

 

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.

 

If you enjoyed this post, subscribe for updates (it's free)

Incredible solution

Thank you so, so, so much!

I'm desperate with save long series of variables by row, and it helps me.

space between line

how to make it in 2 line for below statement.
Currently it shows "SName :" & strSId " CNumber :" & strCId
in dialogbox
i need
"SName :" & strSId
" CNumber :" & strCId

how to do it.

Thanks!

How Can I Print A Continuous

How Can I Print A Continuous Paragraph Using Printer.print
I 'Ve My Paragraph In A Text Box...
I Need to Print It ..
When I Do So ..
Word wrap Is Not supporting ...
entire Para Goes In A single Line...
I need a The Solution For It ???

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.