Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 18

Formatting Debug.Print Messages

The Debug.Print examples so far have been quite simple— Debug.Print followed by a message statement—but the syntax of the message can be somewhat more elaborate than simple text.

For one thing, the position in which the message appears in the Immediate window can be specified by preceding the message text with VB’s Spc() or Tab() functions. If you want to indent a message by 10 spaces, you could do so in either of two ways:

Debug.Print Spc(10) "This message is preceded by ten spaces."

or:

Debug.Print Tab(11) "This message begins on column eleven, which is functionally identical."

Obviously, Spc() inserts spaces in the output, and Tab() sets the position where the next message will appear.

It is also possible to use more than one text expression on the same line:

Dim sDebugMsg as String
sDebugMsg = " will self-destruct in five seconds, Mr.
Phelps."
Debug.Print "This message" & sDebugMsg

When you need to combine multiple text expressions for use with a single Debug.Print statement, the syntax starts to get a bit cluttered. After each text expression, you can tell VB where to put the next
expression. There are three ways to do this.

First, you can place a semicolon after a text expression. This puts the insertion point immediately after the last character displayed.

That is, the first character in the next expression that prints will be immediately after the last character in the expression preceding the semicolon. For all practical purposes, this behavior makes the semicolon act just like the "&" concatenation operator. In fact, the last line of the preceding example could have been written like this:

Debug.Print "This message"; sDebugMsg

Second, you can use the Tab() function to move the insertion point to a specific column. If you want some space between your messages, you might try something like this (if you have an exceptionally wide screen):

Debug.Print "That’s one small step for man"; Tab(100); ";
one mighty leap for VB"

What happens if you specify a Tab position that would cause part of the previous text expression to be overwritten? The first text expression in the preceding example is 29 characters long, for example. What happens if you enter this?

Debug.Print "That’s one small step for man"; Tab(11); ";
one mighty leap to the next line for VB"

You might expect the output in the Immediate window to look like "That’s one;one mighty leap to the next line for VB", starting the second text expression in column 11 of the same line. In fact, VB saves you from such mistakes by moving the second expression to column 11 of the next line.

Finally, you can use Tab with no argument (remember to omit the parentheses too; otherwise, VB will generate a syntax error) to position the insertion point at the beginning of the next print zone. (On average, a print zone occurs about every 14 columns.)

Remember that all the semicolons and Tabs are optional. If you don’t specify where to place the next character, it will print on the next line. You can use these formatting rules to produce output in a variety of ways. However, this example should give you some idea of how to combine them. Figure 18.15 shows the result:

LISTING 18.3
THE Debug.Print MESSAGE

Private Sub cmdOK_Click()
Dim sRef As String, sMsg1 As String, sMsg2 As String
sRef = "12345678901234567890123456789012345678901234567890"
sMsg1 = "I am a string exactly 40 characters long"
sMsg2 = "followed by more text at column 46."
Debug.Print sRef
Debug.Print sMsg1; Spc(5); sMsg2
Debug.Print sMsg1; Tab(46); sMsg2
Debug.Print sMsg1; Tab(Len(sMsg1) + 6); sMsg2; vbCr
Debug.Print "The End!"
End Sub

The output of the Debug.Print formatting example looks like this.
FIGURE 18.15 The output of the Debug.Print formatting example looks like this.

Among other things, notice how it is legal to use a function call and calculations in the Debug.Print line. The Len() function determines how long the sMsg1 string is, and then you add another six columns to that to duplicate the output of the other lines. Why is there an empty line just before "The End!"? That’s because there isn’t a position specifier after the vbCr constant (yes, built-in constants are available, too). You didn’t specify where to place the next character, so it printed on the next line following vbCr.

NOTE - How VB Treats Consecutive Debug.Print Text Expressions: If you try to place two Debug.Print text expressions immediately after one another, VB will insert a semicolon between them for you. In other words, if you type:

Debug.Print "Message #1"
"Message #2"


As soon as you press Enter to move to the next line, VB will automatically change your debugging message into this:

Debug.Print "Message #1";
"Message #2"


  

<< Previous | Contents | Next >>

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved