Visual Basic

Some Visual Basic 6 Tools

We now turn to a discussion of the three Sourcerers mentioned at the beginning of the chapter. These tools-the Assertion Sourcerer, the Metrics Sourcerer, and the Instrumentation Sourcerer-will help you detect and prevent bugs in the programs you write.

Registering the Three Sourcerers


Assert Yourself: The Assertion Sourcerer

Although Debug.Assert fulfills its purpose very well, improvements to it would certainly be welcome. It would be nice if you had the ability to report assertion failures in compiled code as well as source code. Because one of the aims of the Enterprise Edition of Visual Basic 6 is to allow components to be built and then distributed across a network, it is quite likely that others in your organization will want to reference the in-process or out-of-process ActiveX servers that you have built using Visual Basic. Ensuring that assertion failures in compiled Visual Basic 6 programs were reported would be a very useful feature, enabling better testing of shared code libraries and allowing the capture of assertion failures during user acceptance testing. This kind of functionality cannot be implemented using Debug.Assert because these statements are dropped from your compiled program. Additionally, because you cannot drop from object code into Visual Basic's debugger on an assertion failure, you are faced with finding some alternative method of reporting the assertion failures.

Step forward the Assertion Sourcerer. This add-in supplements Debug.Assert with the functionality mentioned above. When you have registered the Sourcerer in the Registry and used the Add-In Manager to reference it, you can select Assertion Sourcerer from the Add-Ins menu to see the window shown in Figure 6-2.

Figure 6-2 The Assertion Sourcerer dialog box

The standard assertion procedure, which supplements the Debug.Assert functionality, is named BugAssert. It is part of a small Visual Basic 6 module named DEBUG.BAS, which you should add to any project in which you want to monitor run-time assertion failures. You can then specify which of your Debug.Assert statements you want converted to run-time assertions; the choices are all assertions in the project or just those in the selected form, class, or module.

The Assertion Sourcerer works in a very simple manner. When you use the Assertion Sourcerer menu option on the Add-Ins menu to request that assertion calls be added to your project, the Assertion Sourcerer automatically generates and adds a line after every Debug.Assert statement in your selected module (or the whole project). This line is a conversion of the Debug.Assert statement to a version suitable for calling the BugAssert procedure. So

  Debug.Assert bTest = True

becomes

  Debug.Assert bTest = True
  BugAssert bTest = True, "bTest = True," _
            "Project Test.VBP, module Test.CLS, line 53"

BugAssert's first argument is just the assertion expression itself. The second argument is a string representation of that assertion. This is required because there is no way for Visual Basic to extract and report the assertion statement being tested from just the first argument. The final argument allows the BugAssert procedure to report the exact location of any assertion failure for later analysis. The BugAssert procedure that does this reporting is relatively simple. It uses a constant to not report assertion failures, to report them to a MsgBox, to report them to a disk file, or to report them to both.

Before compiling your executable, you'll need to set the constant mnDebug in the DEBUG.BAS module. Now whenever your executable is invoked by any other programmer, assertion failures will be reported to the location(s) defined by this constant. Before releasing your code into production, you can tell the Assertion Sourcerer to remove all BugAssert statements from your program.

Some final thoughts You can use the Assertion Sourcerer as a supplement to Debug.Assert when you want to implement assertions in compiled Visual Basic code.