Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Trace routines allow you to track a program as it runs and help you identify lines of code that produce unexpected results. You can add a rudimentary trace routine to your scripts by having the script periodically report its status.
For example, you might echo the value of your variables each time those variables are changed in some way. This enables you to identify any spot in the script where a variable is not assigned the correct value (a common cause of run-time errors). Or you might display a message each time you enter a procedure or function. If a message does not appear, you know that the function or procedure was not called.
The following script projects the number of support calls to a help desk by doing the following:
Assume that there are 93 calls in the first week and 96 in the second week.
The total of calls for the first two weeks is 189.
intWeekOneCalls = InputBox(
"Enter the number of support calls for Week 1:
") intWeekTwoCalls = InputBox(
"Enter the number of support calls for Week 2:
") intTotalCalls = intWeekOneCalls + intWeekTwoCalls intProjectedNumberOfCalls = intTotalCalls * 26 sngCallsPerTechnician = intProjectedNumberOfCalls / 8 Wscript.Echo sngCallsPerTechnician
The script returns a value of 30,537 as the number of calls each technician can expect to handle. Unfortunately, the correct answer is 614.25.
To make matters worse, determining where the problem lies is difficult, because no error messages are returned, and both the scripting syntax and mathematical formulas appear correct.
One solution is to insert a trace routine that periodically reports the value of the variables used in the script. For example, the following revised script uses the Echo statement to display the value of a variable each time that value changes.
intWeekOneCalls = InputBox(
"Enter the number of support calls for Week 1:
") Wscript.Echo intWeekOneCalls intWeekTwoCalls = InputBox(
"Enter the number of support calls for Week 2:
") Wscript.Echo intWeekTwoCalls intTotalCalls = intWeekOneCalls + intWeekTwoCalls Wscript.Echo intTotalCalls intProjectedNumberOfCalls = intTotalCalls * 26 Wscript.Echo intProjectedNumberOfCalls sngCallsPerTechnician = intProjectedNumberOfCalls / 8 Wscript.Echo sngCallsPerTechnician
When this script runs, you will see that the sum of the week 1 and week 2 totals (93 + 96) does not equal 189. Instead, VBScript returns the value 9396. Why? B intWeekTwoCalls ecause it is treating the two variants as string variables rather than integers. As a result, the variables are concatenated (93 & 96 = 9396) and not added (93 + 96 = 189). You have now located the problem.
To solve this problem, you can use the data type features in VBScript to cast these two variables as integers, as shown in the following code snippet:
intWeekOneCalls = InputBox(
"Enter the number of support calls for Week 1:
") intWeekTwoCalls = InputBox(
"Enter the number of support calls for Week 2:
") intTotalCalls = Cint(intWeekOneCalls) + Cint(intWeekTwoCalls) Wscript.Echo intTotalCalls intProjectedNumberOfCalls = Cint(intTotalCalls) * 26 Wscript.Echo intProjectedNumberOfCalls sngCallsPerTechnician = Cint(intProjectedNumberOfCalls) / 8 Wscript.Echo sngCallsPerTechnician
Tip
Send us your feedback | « Previous | Next » |