Stepping through code one line at a time can be tedious if the error is not found quickly. The Immediate window allows you to test program variables and procedures under normal program execution. The Immediate window is displayed by selecting it from the View menu, the Debug toolbar (refer to Figure 7.5), or by pressing Ctrl + G in the IDE.
The Immediate window is often used to hold the value of a variable or variables written to it with debugging statements located at suspected trouble spots in the program. Debugging statements use the Assert() and Print() methods of the Debug object. The Assert() method can be used to break program execution based on a Boolean expression. The Print() method is used to write values to the Immediate window.
Debugging statements are not compiled and stored in the executable program file, so there is no harm in leaving them in your code.
The CalcScore() sub procedure in the Blackjack form module from Chapter 6 is listed below. You may remember that this procedure calculates the scores of the dealer's and player's hands. One of the trickier pieces of this procedure is the part that scores Aces as either one or eleven depending on the value of the hand. The procedure must score the Ace as eleven as long as the player's score does not exceed twenty-one. Several debugging statements have been added to the procedure to test its effectiveness.
In the CalcScore() sub procedure, the expression (numAces = 0) is used with the Assert() method of the Debug object to break program execution. The expression can be any expression that evaluates as true or false, as in this example, or any Boolean variable. The Assert() method breaks program execution when the Boolean expression evaluates as false. In this example, program execution breaks only when an Ace is dealt to either the dealer or player and their hand is scored. This allows you to step through each line of code that calculates the value of the hand based on the number of Aces dealt without having to waste time in Break Mode when no Ace has been dealt. Three statements use the Print() method of the Debug object to write the value of the variable score to the Immediate window before, during, and after the handling of the Aces. It is a good idea to include a string with the Print() method identifying the variable, especially if there are more debugging statements elsewhere in the program. After, or during program execution, the Immediate window and its contents can be viewed from the VBA IDE as shown in Figure 7.6.
Private Sub CalcScore(iPlayer As Integer)
'Calculates the player's and dealer's score. Pass 0 'for the dealer and 1 for the player.
Dim I As Integer Dim numAces As Integer Dim score As Integer Const MAXHANDSIZE = 5
'Calculates the score. Aces count one or eleven.
score = score + scores(I, i Player) If scores(I, i Player) = 1 Then numAces = numAces + 1 Next I
Debug.Assert (numAces = 0) Debug.Print "Score Ace as 1: " & score If (numAces > 0) Then score = score + 10 * numAces Debug.Print "Score Ace as 11: " & score For I = 1 To numAces
If (score > 21) Then score = score - 10 Next I End If
Debug.Print "Final Score: " & score If (iPlayer = 0) Then
IblDlrScore.Caption = score
Else lblPlyrScore.Caption = score End If End Sub
You can also use the Immediate window to enter code statements while the program is in Break Mode. Statements that change the value of a variable, or the property of an ActiveX control, or call a procedure can be entered directly into the Immediate window. The statements take effect after the Enter key is pressed. Using the previous example, the value of the variable score can be changed while in Break Mode by entering score = 5 (or any integer value) in the Immediate window. This is useful for re-directing program execution and testing the results without having to alter code.
Was this article helpful?
Discover the 100 Legal Secrets Las Vegas Professional Blackjack Gamblers Use to Make Literally Thousands of Dollars Every Week... I'll Teach You the Ultimate Step-by-Step Blackjack System, Casino's Don't Want You to Know About! That's Right, I'll show you step-by-step how to have the blackjack odds in your favor, guaranteed!